Nat Luengnaruemitchai

Geek Blog

  Home  |   Contact  |   Syndication    |   Login
  99 Posts | 0 Stories | 212 Comments | 236 Trackbacks

News

Archives

Post Categories

Blogroll

Thursday, September 20, 2007 #

Yesterday, I had a problem where I need to hook up an event to a certain data condition. I first thought about EventTrigger. However, it does the other way around. This class is responsible for convert event into trigger. So I digged around WPF architecture to see what I can do and bingo. I found that I can create an attached property with the following construct

 

 

 

 

and then in XAML, you can register for this event by

<TextBox Text="Try me">
    <
TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <
Style.Triggers>
                <
Trigger Property="Text" Value="Cool">
                    <
Setter Property="me:Window1.SomethingHappened" Value="True" />
                </
Trigger>
            </
Style.Triggers>
        </
Style>
    </
TextBox.Style>
</
TextBox>

 

public static readonly DependencyProperty SomethingHappenedProperty = DependencyProperty.RegisterAttached("SomethingHappened",typeof(bool),typeof(Window1),new PropertyMetadata(false,new PropertyChangedCallback(SomethingHappened)));public bool GetSomethingHappened(DependencyObject d)
{
    return (bool)d.GetValue(SomethingHappenedProperty);
}
public void SetSomethingHappened(DependencyObject d, bool value)
{
    d.SetValue(SomethingHappenedProperty, value);
}
private void SomethingHappened(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    // do something here
}