向 WPF 中的用户控件添加逻辑

本文关键字:添加 控件 用户 WPF | 更新日期: 2023-09-27 18:35:15

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

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

    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,它的定义应该保留在哪里?逻辑应该在控件中编码,还是用户有责任记住属性之间的关系?

向 WPF 中的用户控件添加逻辑

您应该为

UserControl中的C声明另一个DependencyProperty,并将属性更改处理程序添加到AB属性,以便在C的值更改时更新其值。这应该可以完成工作:

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 上的只读依赖项属性页,以获取有关声明只读DependencyProperty的帮助。

你还需要为 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;
        }

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