简体   繁体   中英

How do I bound style property to item value in WPF?

I want to create a TreeView that have 'ItemsSource=ObservableCollection...".
I created a Style for TreeViewItem that contains DataGrid with controls bound to the items in the ObservableCollection. For example CheckBox that bound to a property of the item.

I want to create a Trigger that in case that the item property have specific value it will change property value of the control:

   <Style TargetType="{x:Type TreeViewItem}" x:Key="GridItemStyle" x:Name="GridItemStyle2">
            <Setter Property="Template" >
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TreeViewItem}">                        
                        <Border  Background="Red" BorderBrush="Yellow" BorderThickness="3" Margin="2,10,0,0">
                            <StackPanel Background="Red" Margin="10,10,10,10" Orientation="Horizontal">                                                                                                
                                <StackPanel.Triggers>
                                    <Trigger Property="{Binding IsSucceed}" Value="True">
                                        <Setter Property="Background" Value="Blue"/>
                                    </Trigger>
                                </StackPanel.Triggers>
                                <TextBlock Margin="0,0,10,0" Text="{Binding Path=time, StringFormat={}\{0:dd/MM/yyyy hh:mm:ss:fff\}, Mode=OneWay}"/>
                                <TextBlock Margin="0,0,10,0" Text="{Binding milisecond}"/>
                                <TextBlock Margin="0,0,10,0" Text="{Binding address}"/>
                                <TextBlock Margin="0,0,10,0" Text="{Binding IsSucceed}"/>
                                <TextBlock Margin="0,0,10,0" Text="{Binding statues}"/>
                            </StackPanel>
                        </Border>                       
                    </ControlTemplate>                                      
                </Setter.Value>
            </Setter>
        </Style>

The get exception in runtime because of the Trigger:

  <Trigger Property="{Binding IsSucceed}" Value="True">
       <Setter Property="Background" Value="Blue"/>
  </Trigger>

How do I solve it?

Dont set StackPanel.Triggers , set StackPanel.Style and then set Style.Triggers

  <StackPanel>
        <StackPanel.Style>
            <Style TargetType="StackPanel">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSucceed}" Value="True">
                        <Setter Property="Background" Value="Blue"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </StackPanel.Style>

Change Trigger to DataTrigger , Trigger is used if you are expecting a property (Dependency Properties) from the Control to and use DataTrigger if you are going to bind a property that is bound to a ViewModel which is the DataContext of your Control

Note: DataTriggers can be bind to DependencyProperties too.

<DataTrigger Binding={Binding IsSucceed}" Value="True">
       <Setter Property="Background" Value="Blue"/>
</DataTrigger>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM