Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
XML
<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:

XML
<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

1 solution

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:

XML
<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.
 
Share this answer
 
v3
Comments
Elan.Cao 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 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 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900