Question

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 sophisticated).

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

Kind regards,

Ronald

Was it helpful?

Solution

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top