3

我正在尝试将文本框绑定到依赖属性以更改其宽度。以下代码不起作用。有什么帮助吗?

  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();

        }
    }

这是标记代码:

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

默认值有效,但如果我修改代码中的值没有任何反应?!!!

4

1 回答 1

2

重命名您的依赖属性应该可以解决您的问题

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();
    }
}

希望这可以帮助

于 2012-04-20T07:51:02.997 回答