views:

164

answers:

1

I have created a trigger in the xaml and it works fine. However, when I want to access one of its properties in the code behind it always gives me a null value.

Anyone knows why?

A: 

Sounds like you are referencing the class and not the instantiated trigger.

Take a look at this example. It shows accessing a trigger in C#. Maybe this will get you in the right direction.

The code that should help is the following:

System.Windows.Interactivity.Interaction.GetTriggers

or you could do something like this:

var triggers = this.Element.Triggers;

Here is a small example. Where an EventTrigger is added to a text box named TextBoxInvoker.

    <TextBox x:Name="TextBoxInvoker" Height="33" HorizontalAlignment="Left" VerticalAlignment="Top" Width="123" Text="TextBox" TextWrapping="Wrap">
        <interaction:Interaction.Triggers>
            <interaction:EventTrigger EventName="KeyDown" >
                <behavior:TextBoxEnterButtonInvoke TargetName="TargetedButton" />
            </interaction:EventTrigger>
        </interaction:Interaction.Triggers> 
    </TextBox>

Then to access the trigger via code you can do the following. This will get you the collection of triggers attached to the element.

var triggerCollection = System.Windows.Interactivity.Interaction.GetTriggers(TextBoxInvoker);

or

var triggers = this.TextBoxInvoker.Triggers;
Jason Rowe
Not sure if this is the case...I declared a trigger named MyTrigger in the xaml.And tried to access it (this.MyTrigger) after InitializeComponent But it is always null...
Xin
Thank you so much Jason! This works!!
Xin