WPF UserControl依赖项属性不起作用

时间:2018-02-02 08:19:11

标签: c# wpf xaml user-controls wpf-controls

在WPF项目(下面的代码)中,我有UserControl类型为MyUserControl的带有依赖项属性,名为MyOrientation,类型为Orientation

MainWindow我有MyUserControl的2个实例,其中通过XAML我将Orientation属性设置为Horizontal而将另一个实例设置为Vertical 1}}。

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

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

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

非常感谢提前。

以下是代码:

MYUSERCONTROLVIEWMODEL:

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>

MYUSERCONTROL代码背后:

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>

1 个答案:

答案 0 :(得分:1)

UserControl的“内部”视图模型毫无意义,不应该存在。您应该通过RelativeSource或ElementName Binding直接绑定到依赖项属性:

<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); }
    }
}