Steveluo’s Blog

Just another WordPress.com weblog

WPF basics

Posted by steveluo on March 15, 2009

Dependency Property (Notes from WPF unleashed  p45-57)

dependency properties add value on top of plain .NET properties:
. Change notification  — Whenever the value of a dependency property changes, WPF can automatically
                                                 trigger a number of actions depending on the property’s metadata, see trigger
. Property value inheritance  — flowing of property values down the element tree.
. Support for multiple providers  — a five-step process  (see p57)

    want the text in each Button from the About dialog in  to turn blue when the mouse pointer hovers over it.
normal way with WPF trigger
<Button MouseEnter=”Button_MouseEnter” MouseLeave=”Button_MouseLeave”
             MinWidth=”75” Margin=”10”>Help</Button>
<Button MouseEnter=”Button_MouseEnter” MouseLeave=”Button_MouseLeave”
              MinWidth=”75” Margin=”10”>OK</Button>

void Button_MouseEnter(object sender, MouseEventArgs e)
{
        Button b = sender as Button;
        if (b != null)
              b.Foreground = Brushes.Blue;
}
// Restore the foreground to black when the mouse exits the button
void Button_MouseLeave(object sender, MouseEventArgs e)
{
       Button b = sender as Button;
       if (b != null)
              b.Foreground = Brushes.Black;
}
<Trigger Property=”IsMouseOver” Value=”True”>
      <Setter Property=”Foreground” Value=”Blue”/>
</Trigger>

For example, the data binding system in WPF is rooted in the FrameworkElement.DataContext dependency property.  The value of that property is inherited by all descendant elements. For more see Josh’s blog

Business objects — without  dependency properties  (see the details )  
For a business object being bound, there are certain requirements for automatic change notification. There are three approaches you can take:

  • Implement dependency properties in your business object. 
  • Raise an event named <Property Name>Changed. This event must match the property name, and the class must raise the event.
  • Implement the INotifyPropertyChanged in the System.ComponentModel namespace. This interface defines a PropertyChanged event. The business object must fire this event every time any property value changes.
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: