DependencyProperty 更改 ViewModelClass (UWP) 中的值 [英] DependencyProperty to change value in the ViewModelClass (UWP)

查看:19
本文介绍了DependencyProperty 更改 ViewModelClass (UWP) 中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl,它绘制一些线,其中坐标 (X1,X2,Y1,Y2) 绑定到 ViewModelClass 中的属性,ViewModelClass 本身处理数学以绘制线条以及 UserControl 的 CodeBehind可以设置 ViewModelClass 中绘制线条所需的属性值.以下代码解释了我的控件及其工作原理:

I have a UserControl which draws some lines where the coordinates (X1,X2,Y1,Y2) are bound to properties in the ViewModelClass, the ViewModelClass itself which handles the maths to draw the lines and the CodeBehind for the UserControl where you can set the value of a property which is needed in the ViewModelClass to draw the lines. The following code explains my Control and how it works:

用户控件.xaml

<Line x:Name="StartAngleLine" X1="{Binding Path=StartAngleX1}" X2="{Binding Path=StartAngleX2}" Y1="{Binding Path=StartAngleY1}" Y2="{Binding Path=StartAngleY2}" Stroke="Aqua" StrokeThickness="6"/>

UserControl.xaml.cs

UserControl.xaml.cs

public Constructor()
{
    private readonly ViewModel model = new ViewModel();
    DataContext = model;
}

public int StartAngle
{
    get { return model.StartAngle; }
    set { model.StartAngle = value; }
}

ViewModel.cs

ViewModel.cs

public int StartAngle
{
    get
    {
        return startAngle;
    }

    set
    {
        if (value != startAngle)
        {
            if (value >= 0 && value <= 360)
            {
                startAngle = value;
                NotifyPropertyChanged();
                StartAngleChanged();
            }
            else
            {
                throw new ArgumentOutOfRangeException($"StartAngle", "Angle is out of range.");
            }
        }
    }
}

public double StartAngleX1
{
    get
    {
        startAngleX1 = centerX + (centerX1 * Math.Cos(StartAngle * (Math.PI / 180)));
        return startAngleX1;
    }
}

private void StartAngleChanged()
{
    NotifyPropertyChanged("StartAngleX1");
    NotifyPropertyChanged("StartAngleX2");
    NotifyPropertyChanged("StartAngleY1");
    NotifyPropertyChanged("StartAngleY2");
}

如何在我的 UserControl.xaml.cs 中设置 DependencyProperties(例如 StartAngleProperty 而不是 StartAngle,如 UserControl.xaml.cs 中所示)并仍然让它们更改 ViewModelClass 中的属性?还是最好将它们保留在 CodeBehind 中并将 ViewModelClass 中的属性更改为 DependencyProperties?

How can i set DependencyProperties (e.g. StartAngleProperty instead of StartAngle as shown in UserControl.xaml.cs) in my UserControl.xaml.cs and still make them change the Property in the ViewModelClass? Or is it better to leave things like they are in the CodeBehind and change the Properties in the ViewModelClass to DependencyProperties?

推荐答案

您可以像这样在 UserControl 中声明一个依赖属性:

You could declare a dependency property in your UserControl like this:

public static readonly DependencyProperty StartAngleX1Property =
    DependencyProperty.Register(
        "StartAngleX1",
        typeof(double),
        typeof(MyUserControl),
        new PropertyMetadata(0.0));

public double StartAngleX1
{
    get { return (double)GetValue(StartAngleX1Property); }
    set { SetValue(StartAngleX1Property, value); }
}

并在 UserControl 的 XAML 中像这样绑定到它:

and bind to it in the UserControl's XAML like this:

<UserControl ... x:Name="self">
    ...
    <Line X1="{Binding StartAngleX1, ElementName=self}" .../>
    ...
</UserControl>

那么就不需要在您的 UserControl 中拥有私有视图模型.当您使用它时,您将改为绑定 UserControl 的属性,例如

Then there is no need to have a private view model in your UserControl. You would instead bind the UserControl's properties when you use it, like

<mycontrol:MyUserControl StartAngleX1="{Binding SomeViewModelPropery}" ... />

这篇关于DependencyProperty 更改 ViewModelClass (UWP) 中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆