Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: WPF Style trigger
There's a datagrid in my from,and I ues a DataTrigger to set the DataGridCell's foreground color to ensure that it looks clear,just like this:
<Style TargetType="my:DataGridCell">
     <Style.Triggers>
          <DataTrigger Binding="{Binding Path=State}" Value="0">
               <Setter Property="Foreground" Value="Black" />
          </DataTrigger>
          <DataTrigger Binding="{Binding Path=State}" Value="1">
               <Setter Property="Foreground" Value="Green" />
          </DataTrigger>
     </Style.Triggers>
</Style>
 
And then I want to set it's StyleTrigger,to make sure they have same background and BorderBrush,just like this:
 
<Style.Triggers >
    <Trigger Property="IsSelected" Value="True">
        <Setter Property="Background" Value="#d4ecff"/>
        <Setter Property="BorderThickness" Value="0"></Setter>
        <Setter Property="BorderBrush" Value="#d4ecff"></Setter>
    </Trigger>
</Style.Triggers>
 
but then I have a question,when I selected one cell,the cell foreground change White because I didn't set it so it been set a acquiescent color.
I want to achieve this:whichever cell i select,the cell background will change same(#d4ecff),but the foreground will not change:If it's Green,it keeps Green and if it's Black ,it keeps Black.
 
How Can I do this ? thank you everyone helps me!
Posted 16-Jan-13 16:38pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,
 
I think you can achieve this using a MultiDataTrigger for the IsSelected property
(in addition to the DataTrigger for the State property) like so:
 
<MultiDataTrigger>
   <MultiDataTrigger.Conditions>
      <Condition Binding="{Binding Path=IsSelected}" Value="True" />
      <Condition Binding="{Binding Path=State}" Value="0" />
   </MultiDataTrigger.Conditions>
   <MultiDataTrigger.Setters>
      <Setter Property="Background" Value="#d4ecff" />
      <Setter Property="BorderThickness" Value="0" />
      <Setter Property="BorderBrush" Value="#d4ecff" />
      <Setter Property="Foreground" Value="Black" />
   </MultiDataTrigger.Setters>
</MultiDataTrigger>
 
<MultiDataTrigger>
   <MultiDataTrigger.Conditions>
      <Condition Binding="{Binding Path=IsSelected}" Value="True" />
      <Condition Binding="{Binding Path=State}" Value="1" />
   </MultiDataTrigger.Conditions>
   <MultiDataTrigger.Setters>
      <Setter Property="Background" Value="#d4ecff" />
      <Setter Property="BorderThickness" Value="0" />
      <Setter Property="BorderBrush" Value="#d4ecff" />
      <Setter Property="Foreground" Value="Green" />
   </MultiDataTrigger.Setters>
</MultiDataTrigger>
 
 
Bye, Thomas.
  Permalink  
v3
Comments
Elan.Cao at 17-Jan-13 4:43am
   
Thank You Thomas,but i'm sorry it doesn't work."DataTrigger" just take effect when Binding data.
Thomas Duwe at 17-Jan-13 4:53am
   
DataTrigger can be bound to any property (CLR and dependency property, whereas class containing CLR property needs to implement INotifyPropertyChanged interface)
Trigger can be bound only to Dependency Property.
Do you tried it?
 
Or try instead of Condition Binding={Binding Path=IsSelected...} -> Property=IsSelected
Elan.Cao at 17-Jan-13 7:10am
   
eh...My propert didn't implement INotifyPropertyChanged interface yet...I'll try,and thank you Thomas very much!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 17 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100