【问题标题】:DependencyProperty to change value in the ViewModelClass (UWP)DependencyProperty 更改 ViewModelClass (UWP) 中的值
【发布时间】:2017-11-22 18:27:06
【问题描述】:

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

UserControl.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

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

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

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 而不是如 UserControl.xaml.cs 中所示的 StartAngle)并仍然让它们更改 ViewModelClass 中的属性?还是将它们留在 CodeBehind 中并将 ViewModelClass 中的属性更改为 DependencyProperties 更好?

【问题讨论】:

    标签: c# xaml mvvm uwp


    【解决方案1】:

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

    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 中绑定到它:

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

    那么就没有必要在你的 UserControl 中有一个私有视图模型。相反,当你使用它时,你会绑定 UserControl 的属性,比如

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

    【讨论】:

    • 我看到这是一种可能性,但这不是使用 ViewModelClass 来减少 xaml.cs 中的代码量的原因吗?
    • 应该有一个视图模型的原因有很多。但是,UserControl 不应该有自己的私有视图模型实例。相反,它应该对通过 DataContext 属性从其环境继承的视图模型进行操作,例如从父控件或页面或窗口。
    • 您当然可以在后面的 UserControl 代码中进行一些登录。例如。 StartAngle 属性也可以是 UserControl 的依赖属性,并且控件可以对 PropertyChangedCallback(由 PropertyMetadata 传递)中的值更改做出反应。然后它可能会更新其其他属性的值。
    • 至于第一条评论,谢谢提示,我不知道!对于第二条评论,我将获取 xaml.cs 的属性并将 StartAngle 作为 DependencyProperty。如果 StartAngle 使用 PropertyChangedCallback 将其更改调用为 StartAngleX1、StartAngleX2 等,它会是什么样子?
    • VisualStudio dep tab,你可以看到上面。
    猜你喜欢
    • 2011-01-04
    • 2019-06-01
    • 2019-01-16
    • 1970-01-01
    • 2017-04-14
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 2013-11-02
    相关资源
    最近更新 更多