Using C# dependency property in XAML Triggers
-
While creating style for a button I am writing like this:
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Image Name="Normal" Source="Resources/Images/n0.png"/> <Image Name="RollOver1" Source="Resources/Images/r1.png" Visibility="Hidden"/> <Image Name="RollOver2" Source="Resources/Images/r2.png" Visibility="Hidden"/> <Image Name="RollOver3" Source="Resources/Images/r3.png" Visibility="Hidden"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/> <Setter TargetName="RollOver1" Property="Visibility" Value="Visible"/> </Trigger>
Can we control this trigger by some global variable/ dependency property defined in c# code? So, we would be able to use it with Multi-trigger? Basically my requirement is to display different images based on property "DepProp". I need to be able to wrtite something like this:
Is it possible? Other way around would be to access these style objects on mouseenter in c# code. Possible? If yes, how? Which way will be better?
-
While creating style for a button I am writing like this:
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Image Name="Normal" Source="Resources/Images/n0.png"/> <Image Name="RollOver1" Source="Resources/Images/r1.png" Visibility="Hidden"/> <Image Name="RollOver2" Source="Resources/Images/r2.png" Visibility="Hidden"/> <Image Name="RollOver3" Source="Resources/Images/r3.png" Visibility="Hidden"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/> <Setter TargetName="RollOver1" Property="Visibility" Value="Visible"/> </Trigger>
Can we control this trigger by some global variable/ dependency property defined in c# code? So, we would be able to use it with Multi-trigger? Basically my requirement is to display different images based on property "DepProp". I need to be able to wrtite something like this:
Is it possible? Other way around would be to access these style objects on mouseenter in c# code. Possible? If yes, how? Which way will be better?
I have the same problem: I need to put lines with different colors in a datagrid base on what happens in code behind. I think one solution will be to attach a style with a trigger from the code behind. Of course I wait to see how can access a code declared dependency property from xaml. Maybe with a DataTrigger.
-
While creating style for a button I am writing like this:
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Image Name="Normal" Source="Resources/Images/n0.png"/> <Image Name="RollOver1" Source="Resources/Images/r1.png" Visibility="Hidden"/> <Image Name="RollOver2" Source="Resources/Images/r2.png" Visibility="Hidden"/> <Image Name="RollOver3" Source="Resources/Images/r3.png" Visibility="Hidden"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/> <Setter TargetName="RollOver1" Property="Visibility" Value="Visible"/> </Trigger>
Can we control this trigger by some global variable/ dependency property defined in c# code? So, we would be able to use it with Multi-trigger? Basically my requirement is to display different images based on property "DepProp". I need to be able to wrtite something like this:
Is it possible? Other way around would be to access these style objects on mouseenter in c# code. Possible? If yes, how? Which way will be better?
I wouldn't approach it like this. A simpler way to do this would be to implement a value converter which did the heavy work of just displaying the appropriate image, rather than showing and hiding elements.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
While creating style for a button I am writing like this:
<Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="{x:Type Button}"> <Grid> <Image Name="Normal" Source="Resources/Images/n0.png"/> <Image Name="RollOver1" Source="Resources/Images/r1.png" Visibility="Hidden"/> <Image Name="RollOver2" Source="Resources/Images/r2.png" Visibility="Hidden"/> <Image Name="RollOver3" Source="Resources/Images/r3.png" Visibility="Hidden"/> </Grid> <ControlTemplate.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Setter TargetName="Normal" Property="Visibility" Value="Hidden"/> <Setter TargetName="RollOver1" Property="Visibility" Value="Visible"/> </Trigger>
Can we control this trigger by some global variable/ dependency property defined in c# code? So, we would be able to use it with Multi-trigger? Basically my requirement is to display different images based on property "DepProp". I need to be able to wrtite something like this:
Is it possible? Other way around would be to access these style objects on mouseenter in c# code. Possible? If yes, how? Which way will be better?
I have solved my problem like here: http://msdn.microsoft.com/en-us/library/system.windows.datatrigger.aspx http://msdn.microsoft.com/en-us/library/ms668604.aspx and I can put different colors in my grid based on the normal property from the Item class.
-
I wouldn't approach it like this. A simpler way to do this would be to implement a value converter which did the heavy work of just displaying the appropriate image, rather than showing and hiding elements.
*pre-emptive celebratory nipple tassle jiggle* - Sean Ewington
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
Thanks! Yes your approach is better one. I opted for INotifyPropertyChanged and sorted out my issue. :)
-
I have solved my problem like here: http://msdn.microsoft.com/en-us/library/system.windows.datatrigger.aspx http://msdn.microsoft.com/en-us/library/ms668604.aspx and I can put different colors in my grid based on the normal property from the Item class.
Thanks for sharing those links. Meanwhile I sorted out my requirement using 'INotifyPropertyChanged'.