user342552
user342552

Reputation:

WPF Binding Expression and Dependency property

I am trying to bind a text box to a dependency property to change its width. The following code is not working. Any help?

  public class ToolbarManager : DependencyObject
    {
        public static readonly DependencyProperty toolbarButtonWidth = DependencyProperty.Register("toolbarButtonWidth", typeof(Double), typeof(ToolbarManager), new FrameworkPropertyMetadata(32.0, FrameworkPropertyMetadataOptions.AffectsMeasure ));
        public static readonly DependencyProperty toolbarButtonHeight = DependencyProperty.Register("toolbarButtonHeight", typeof(Double), typeof(ToolbarManager), new FrameworkPropertyMetadata(32.0, FrameworkPropertyMetadataOptions.AffectsMeasure));

        public double ButtonWidth
        {
            get { return (double)GetValue(toolbarButtonWidth); }
            set { SetValue(toolbarButtonWidth, value); }
        }

        public double ButtonHeight
        {
            get { return (double)GetValue(toolbarButtonHeight); }
            set { SetValue(toolbarButtonHeight, value); }
        }

        public static ToolbarManager Instance { get; private set; }

        static ToolbarManager()
        {
            Instance = new ToolbarManager();

        }
    }

Here is the markup code:

<TextBox Width="{Binding Source={x:Static local:ToolbarManager.Instance}, Path=ButtonWidth, Mode=OneWay}" />

The default value works, but if I modify the value in code nothing happens ?!!!

Upvotes: 3

Views: 727

Answers (1)

punker76
punker76

Reputation: 14621

rename your dependency property should solve your problem

public class ToolbarManager : DependencyObject
{
    public static readonly DependencyProperty ButtonWidthProperty =
      DependencyProperty.Register("ButtonWidth", typeof(Double), typeof(ToolbarManager), new FrameworkPropertyMetadata(32.0, FrameworkPropertyMetadataOptions.AffectsMeasure ));
    public static readonly DependencyProperty ButtonHeightProperty =
      DependencyProperty.Register("ButtonHeight", typeof(Double), typeof(ToolbarManager), new FrameworkPropertyMetadata(32.0, FrameworkPropertyMetadataOptions.AffectsMeasure));

    public double ButtonWidth
    {
        get { return (double)GetValue(ButtonWidthProperty); }
        set { SetValue(ButtonWidthProperty, value); }
    }

    public double ButtonHeight
    {
        get { return (double)GetValue(ButtonHeightProperty); }
        set { SetValue(ButtonHeightProperty, value); }
    }

    public static ToolbarManager Instance { get; private set; }

    static ToolbarManager()
    {
        Instance = new ToolbarManager();
    }
}

hope this helps

Upvotes: 2

Related Questions