0

我正在创建一个具有三个属性的简单用户控件。为简单起见,我们假设它们是 A、B 和 C。此外,C = A + B。我想在 TextBoxes 中显示所有这些(A、B - 用户可编辑,C - 只读)。每当用户修改 A 或 B 时,应更新 C 的值。

我已经在 MyControl.xaml.cs 文件中为 A 和 B 创建了依赖属性。

    public static readonly DependencyProperty AProperty =
            DependencyProperty.Register("A", typeof(double),
            typeof(MyControl), new FrameworkPropertyMetadata(0.0));

    public double A
    {
        get { return (double)GetValue(AProperty); }
        set { SetValue(AProperty, value); }
    }

    public static readonly DependencyProperty BProperty =
            DependencyProperty.Register("B", typeof(double),
            typeof(MyControl), new FrameworkPropertyMetadata(0.0));

    public double B
    {
        get { return (double)GetValue(BProperty); }
        set { SetValue(BProperty, value); }
    }

我的问题是:我应该如何处理 C 以及它的定义应该保存在哪里?逻辑应该在控件中编码,还是用户有责任记住属性之间的关系?

4

2 回答 2

2

您应该在和 中声明另一个DependencyPropertyfor并将属性更改处理程序添加到您的和属性以更新其值更改时的值。这应该做的工作:CUserControlABC

public static readonly DependencyProperty AProperty =
    DependencyProperty.Register("A", typeof(double),
    typeof(MyControl), new UIPropertyMetadata(0.0, OnAOrBPropertyChanged));

public static readonly DependencyProperty BProperty =
    DependencyProperty.Register("B", typeof(double),
    typeof(MyControl), new UIPropertyMetadata(0.0, OnAOrBPropertyChanged));

public static void OnAOrBPropertyChanged(DependencyObject dependencyObject, 
    DependencyPropertyChangedEventArgs e)
{
    dependencyObject.SetValue(CProperty, (double)dependencyObject.GetValue(AProperty) +
        (double)dependencyObject.GetValue(BProperty));
}

您可能想查看 MSDN 上的Read-Only Dependency Properties页面以获取有关声明只读DependencyProperty.

于 2013-10-13T22:08:53.733 回答
1

您还需要为 C 创建另一个依赖属性

你可以试试这个:

public static readonly DependencyProperty AProperty =
            DependencyProperty.Register("A", typeof(double),
            typeof(MyControl), new FrameworkPropertyMetadata(OnAorBChange));

        public double A
        {
            get { return (double)GetValue(AProperty); }
            set { SetValue(AProperty, value); }
        }

        public static readonly DependencyProperty BProperty =
                DependencyProperty.Register("B", typeof(double),
                typeof(MyControl), new FrameworkPropertyMetadata(OnAorBChange));

        public double B
        {
            get { return (double)GetValue(BProperty); }
            set { SetValue(BProperty, value); }
        }

        public static readonly DependencyProperty CProperty =
           DependencyProperty.Register("C", typeof(double),
           typeof(MyControl), new FrameworkPropertyMetadata(null));

        public double C
        {
            get { return (double)GetValue(CProperty); }
            set { SetValue(CProperty, value); }
        }

        private static void OnAorBChange(DependencyObject obj,
           DependencyPropertyChangedEventArgs args)
        {
            var obj1 = obj as MyControl;
            obj1.C = obj1.A + obj1.B;
        }

我假设你做了正确的绑定:)

于 2013-10-14T07:25:26.397 回答