locked
Trying to Assign a ViewModel Property to DataTrigger.Value RRS feed

  • Question

  • Hi,

    I am trying to assign a View Model property to <DataTrigger>'s "Value" Property. And I am getting below Error:

    "A 'Binding' cannot be set on the 'Value' property of type 'DataTrigger'. A 'Binding' can only be set on a DependencyProperty of a DependencyObject.".

    And, here is my code snippet:

    <DataTrigger Binding="{Binding Path=[Short_Name]}" Value="{Binding Path=Test}">

          <Setter Property="Background" Value="Red" />

    </DataTrigger>

    where "Test" is my VIewModel's property, and will be set dynamically.

     

    Thanks,

    Balaji

    Thursday, May 12, 2011 4:15 PM

Answers

  • Just checking in to see if the information was helpful. Please let us know if you would like further assistance.

    Have a great day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by BalajiTE Monday, May 23, 2011 1:27 AM
    Thursday, May 19, 2011 2:43 AM

All replies

  • Hi Balaji,

    You can't bind to the Value property of a DataTrigger, because (as the error says), Value is not a dependency property.

    Instead of checking to see if Short_Name has the value found in Test, you can create a more complicated binding expression that compares Short_Name to Test and then check to see if the result of that comparison is "True".

    Take a look at the following example in Stack Overflow - http://stackoverflow.com/questions/2239839/wpf-using-bindings-in-data-trigger-condition

     

    Sean

    Thursday, May 12, 2011 5:23 PM
  • Hi Sean,

    Thank you for the quick response. I gone through the example and tried to implement but still not getting the resultant color in the data row. Below is the code what I updated based on the example:

    <DataTrigger Value="True">

         <DataTrigger.Binding>

                    <MultiBinding Converter={StaticResource AvgConverter}">

                              <Binding Path="[Short_Name]" />

                               <Binding Path="Test" />

                     <MultiBinding>

         </DataTrigger.Binding>

         <Setter Property="Background" Value="Red" />

    </DataTrigger>

     

    In the AvgConverter class I am return the value as "XzQ" (which matches with my column data).

    Can you let me know what I am doing wrong and what should be there in converter class?

    Thanks,

    Balaji

    Thursday, May 12, 2011 6:30 PM
  • The statement above says, "When binding "[Short_Name]" and "Test" using the AvgConverter results in True, set the Background to Brushes.Red"  If you are returning XzQ from the converter, XzQ != True so it will not evaluate the Setter.
    Thursday, May 12, 2011 8:17 PM
  • Your converter could be somthing like this,

     public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
          if(values.Length >1 && values[0]!= null)
          {
            return values[0].Equals(values[1]);
          }
    
    
          return null;
        }
    


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, May 13, 2011 3:12 AM
  • Hi Min,

    Thanks for your response. I implemented the logic in converter class, but the viewmodel property ("Test") is not getting the value, in converter the value of it is showing as "DependencyProperty.UnsetValue".

    My property definition is as below in viewmodel: 

    private string _test;

    public string Test

    {

        get { return _test; }

        set { _test = value; } 

    }

    Any suggestions why the ViewModel property value is not getting passed to the converter?

     

    - Balaji

     

    Friday, May 13, 2011 1:36 PM
  • Hi BalajiTE,

    Thanks for the update.

    Is the data binding for this property set up correctly? Could you please provide more details about the binding including the relevant code in view and viewmodel?

    Also, it will be very helpful if you can provide a working sample for us to reproduce the problem.

    Best regards,


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 16, 2011 8:26 AM
  • Hello Min,

    Here is my code:

    View:

             <Style TargetType="{x:Type xcdg:DataRow}">

                    <Style.Trigger>

                            <DataTrigger Value="True">

                                    <MultiBinding Converter="{StaticResource DupPlayListItemConverter}">

                                           <Binding Path="Short_Name" />

                                           <Binding Path="Test" /> 

                                     </MultiBinding>

                                    <Setter Property="Background" Value="Red" />

                            </DataTrigger> 

                     </Style.Trigger>

             </Style>

     

    ViewModel:

    Private string _test;

    Public string Test

    {

          get { return _test; }

          set { _test = value; }

    }

     

    The Binding to ViewModel to View i s working fine as other properties are all getting bound properly. Hope this helps for you to understand my problem.

     

    Thanks,

    Balaji

     

    Monday, May 16, 2011 1:30 PM
  • Hi Balaji,

    Try this,

          <Style TargetType="{x:Type xcdg:DataRow}">
            <Style.Triggers>
              <DataTrigger Value="True">
                <DataTrigger.Binding>
                  <MultiBinding Converter="{StaticResource DupPlayListItemConverter}">
                    <Binding Path="Short_Name" />
                    <Binding Path="Test" />
                  </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Background" Value="Red" />
              </DataTrigger>
            </Style.Triggers>
          </Style>
    

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 17, 2011 6:12 AM
  • Just checking in to see if the information was helpful. Please let us know if you would like further assistance.

    Have a great day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by BalajiTE Monday, May 23, 2011 1:27 AM
    Thursday, May 19, 2011 2:43 AM
  • Hello Min,

    I got the solution for my problem. It is not related to the .xaml what I defined but the way I am using the item source of the control. Thank you for your support in finding the solution.

    Thanks,

    Balaji
    Monday, May 23, 2011 1:28 AM