Windows Vista Forums

Binding to a custom dependency property
  1. #1


    diffeq Guest

    Binding to a custom dependency property

    When the source's "MyColor" property is changed, the GraphPanel's "AxisColor"
    is not changed.

    This binding works fine if I use one of GraphPanel's built-in dependency
    properties, such as Tag.

    -----------------------------------------------



    public class MySource : INotifyPropertyChanged
    {
    ....
    public SolidColorBrush MyColor {...}
    ....
    }

    -----------------------------------------------

    <Grid> <!-- DataContext set in code to MySource -->
    ....
    <GraphPanel : DockPanel>
    <Binding Path="MyColor">
    </GraphPanel>
    ....
    </Grid>

    ------------------------------------------------

    public class GraphPanel
    {
    ....
    public static readonly DependencyProperty AxisColorProperty =
    DependencyProperty.Register(
    "AxisColor",
    typeof(SolidColorBrush),
    typeof(GraphPanel),
    new PropertyMetadata(Brushes.Black));

    public SolidColorBrush AxisColor
    {
    get { return (SolidColorBrush)this.GetValue(AxisColorProperty); }
    set { this.SetValue(AxisColorProperty, value); }
    }
    ....
    }

      My System SpecsSystem Spec

  2. #2


    diffeq Guest

    Mystery Solved

    I found the following at
    http://www.quepublishing.com/article...seqNum=2&rl=1:


    WARNING: .NET property wrappers are bypassed at run-time when setting
    dependency properties in XAML!

    Although the XAML compiler depends on the property wrapper at compile-time,
    at run-time WPF calls the underlying GetValue and SetValue methods directly!
    Therefore, to maintain parity between setting a property in XAML and
    procedural code, it's crucial that property wrappers do not contain any logic
    in addition to the GetValue/SetValue calls. If you want to add custom logic,
    that's what the registered callbacks are for. All of WPF's built-in property
    wrappers abide by this rule, so this warning is for anyone writing a custom
    class with its own dependency properties.



    "diffeq" wrote:

    > When the source's "MyColor" property is changed, the GraphPanel's "AxisColor"
    > is not changed.
    >
    > This binding works fine if I use one of GraphPanel's built-in dependency
    > properties, such as Tag.
    >
    > -----------------------------------------------
    >
    > public class MySource : INotifyPropertyChanged
    > {
    > ...
    > public SolidColorBrush MyColor {...}
    > ...
    > }
    >
    > -----------------------------------------------
    >
    > <Grid> <!-- DataContext set in code to MySource -->
    > ...
    > <GraphPanel : DockPanel>
    > <Binding Path="MyColor">
    > </GraphPanel>
    > ...
    > </Grid>
    >
    > ------------------------------------------------
    >
    > public class GraphPanel
    > {
    > ...
    > public static readonly DependencyProperty AxisColorProperty =
    > DependencyProperty.Register(
    > "AxisColor",
    > typeof(SolidColorBrush),
    > typeof(GraphPanel),
    > new PropertyMetadata(Brushes.Black));
    >
    > public SolidColorBrush AxisColor
    > {
    > get { return (SolidColorBrush)this.GetValue(AxisColorProperty); }
    > set { this.SetValue(AxisColorProperty, value); }
    > }
    > ...
    > }


      My System SpecsSystem Spec

Binding to a custom dependency property problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Use my custom TypeDescriptor to obtains default Value on property inXAML Property Editor of Visual Studio 2008 azerty Avalon 0 14 Apr 2008
WPF: Autogenerate a Private Dependency Property ? Jules Winfield .NET General 4 06 Mar 2008
Custom Dependency Property in custom class hierarchy not workingcorrectly?! MueMeister Avalon 0 02 Mar 2006
Dependency property not initialized in OnPropertyChanged? Jared Bienz Avalon 0 10 Jan 2006
Custom Dependency Property and Binding CSkinner Avalon 3 10 Jan 2006