views:

276

answers:

1

Hi,

I'd like to have a Silverlight behavior that is triggered by a change to a property in the view model for my page. I can't figure out how to do this, however.

So, I have a very simple view model:

public class MyViewModel : INotifyPropertyChanged
{
    private bool changingProperty;
    public bool ChangingProperty
    {
        get { return changingProperty; }
        set
        {
            if (changingProperty != value)
            {
                changingProperty = value;
                NotifyPropertyChanged("ChangingProperty");
            }
        }
    }
    public string SomeProperty { get { return "SomePropertyValue"; } }

    // INotifyPropertyChanged implementation here.......
}

This view model is the data context for a user control that has a text block bound to SomeProperty:

<TextBlock x:Key="myTextBlock" Text="{Binding SomeProperty}" />

This all works fine. Now I'd like to attach a behavior to myTextBlock that is triggered by changes to ChangingProperty in my view model. The behavior should highlight the TextBlock, for example (or something more sofisticated).

How do I specify this trigger? Is this possible at all?

Kind regards,

Ronald

+1  A: 

Something along the lines of my answer to a similar issue here might help.

Here is an example of how you might apply that technique to your requirement.

<Grid.Resources>
   <local:BoolToBrushConverter x:Key="Highlighter"
 FalseBrush="Transparent" TrueBrush="Yellow" />
</Grid.Resources>

<Border Background="{Binding ChangingProperty, Converter={StaticResource Highlighter}}">
 <TextBlock x:Name="txtTarget" Text="{Binding SomeProperty}" />
</Border>
AnthonyWJones
Yes, I've over-simplified my requirements. What I'd like to do for example is highlight the text in the TextBlock, or run some animation (or anything else that you can do with a Silverlight behavior). I updated my question to reflect this.
Ronald Wildenberg
That may work. Not exactly what I wanted but I think this is the best you can do with Silverlight at the moment. I'll give it a try, thanks. Any idea if the WPF DataTrigger will be part of Silverlight 4? I think that would solve my problem.
Ronald Wildenberg
Sadly I don't think SL4 has this either.
AnthonyWJones