WPF 用户控件依赖项 属性 不工作

WPF UserControl Dependency Property not working

在一个 WPF 项目(下面的代码)中,我有一个 UserControl 类型 MyUserControl 具有依赖性 属性,称为 MyOrientation 类型 Orientation .

MainWindow 上我有 2 个 MyUserControl 实例,其中通过 XAML 我将 Orientation 属性 设置为 Horizontal 另一个实例是 Vertical.

我已将 MyOrientation 属性 设置为 DP,因为我希望能够像本示例中那样直接在 XAML 中设置它或使用绑定。

我的问题是,当我 运行 项目时,UserControl 的两个实例都显示 Orientation = Horizo​​ntal?

有人可以告诉我我做错了什么以及如何解决吗?

非常感谢。

代码如下:

我的用户控件视图模型:

public class MyUserControlViewModel : ViewModelBase
{
    private Orientation _myOrientation;
    public Orientation MyOrientation
    {
        get { return _myOrientation; }
        set
        {
            if (_myOrientation == value)
                return;
            _myOrientation = value;
            OnPropertyChanged();
        }
    }

}

MYUSERCONTROL.XAML

<UserControl x:Class="TestUserControlDPProblem.MyUserControl"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:TestUserControlDPProblem"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="300">
<Grid x:Name="root">

    <Grid.DataContext>
        <local:MyUserControlViewModel/>
    </Grid.DataContext>

    <StackPanel Orientation="{Binding MyOrientation}">
        <TextBlock>Hello</TextBlock>
        <TextBlock>There</TextBlock>
    </StackPanel>
</Grid>

我的用户控制代码在后面:

public partial class MyUserControl : UserControl
{
    MyUserControlViewModel _vm;

    public MyUserControl()
    {
        InitializeComponent();

        _vm = root.DataContext as MyUserControlViewModel;
    }

    public static readonly DependencyProperty MyOrientationProperty = DependencyProperty.Register("MyOrientation", typeof(Orientation), typeof(MyUserControl), new FrameworkPropertyMetadata(Orientation.Vertical, new PropertyChangedCallback(OnMyOrientationChanged)));

    private static void OnMyOrientationChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        var myUserControl = o as MyUserControl;
        myUserControl?.OnMyOrientationChanged((Orientation)e.OldValue, (Orientation)e.NewValue);
    }

    protected virtual void OnMyOrientationChanged(Orientation oldValue, Orientation newValue)
    {
        _vm.MyOrientation = newValue;
    }
    public Orientation MyOrientation
    {
        get
        {
            return (Orientation)GetValue(MyOrientationProperty);
        }
        set
        {
            SetValue(MyOrientationProperty, value);
        }
    }

}

MAINWINDOW.XAML

<Window x:Class="TestUserControlDPProblem.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:TestUserControlDPProblem"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel>
        <local:MyUserControl Margin="10" MyOrientation="Horizontal"/>
        <local:MyUserControl Margin="10" MyOrientation="Vertical"/>
    </StackPanel>


</Grid>

UserControl 的 "internal" 视图模型毫无意义,不应存在。您应该改为通过 RelativeSource 或 ElementName 绑定直接绑定到依赖项 属性:

<StackPanel Orientation="{Binding MyOrientation,
                          RelativeSource={RelativeSource AncestorType=UserControl}}">

您甚至不需要 PropertyChangedCallback:

public partial class MyUserControl : UserControl
{
    public MyUserControl()
    {
        InitializeComponent();
    }

    public static readonly DependencyProperty MyOrientationProperty =
        DependencyProperty.Register(
            nameof(MyOrientation), typeof(Orientation), typeof(MyUserControl), 
            new FrameworkPropertyMetadata(Orientation.Vertical));

    public Orientation MyOrientation
    {
        get { return (Orientation)GetValue(MyOrientationProperty); }
        set { SetValue(MyOrientationProperty, value); }
    }
}