1
votes

Trying add a Trigger to a Style programmatically. I want the property for the Trigger Setter to be "Template". I also want the Setter Value to be a DynamicResource-

This is what my .xaml looks like, I want to add the trigger programmatically though:

<Style x:Key="ContentStyle" TargetType="{x:Type ListBoxItem}">
   <Style.Triggers>
      <Trigger Property="IsMouseOver" Value="true">
         <Setter
            Property="Template"
            Value="{DynamicResource HoverStyle1}" />
      </Trigger>
   </Style.Triggers>
</Style>

This is what I have so far in C#, which does not compile, namely the setter.Propery and setter.Value lines:

Style myStyle = null;
myStyle = listboxPatientList.TryFindResource("ContentStyle") as Style;

Setter setter = new Setter();
setter.Property = UIElement.Template;
setter.Value = {DynamicResource HoverPatientFull};

Trigger trigger = new Trigger();
trigger.Property = UIElement.IsMouseOverProperty;
trigger.Value = true;
trigger.Setters.Add(setter);

myStyle.Triggers.Add(trigger);

My reasoning for wanting to do this programmatically is because my system is having trouble specifying between mouseevents and touchevents. I've found a way programmatically to determine input device, depending on that situation this trigger should be added or not present. If the trigger is present when there is a touchevent, it does not register it as a mouseclick, just a mouseover.

1
maybe write a multitrigger? one for touchDevice=false + ismouseover=true; and another for touchDevice=true + touchproperty=true; - ASh

1 Answers

3
votes

First, to set the Template property in code, use

setter.Property = Control.TemplateProperty;

Second, to access a resource, try

setter.Value = FindResource("HoverPatientFull") as /*Whatever data type the resource is*/;