首页 > 解决方案 > DependencyProperty 未设置其值

问题描述

我有以下课程:

主窗口.xaml

<Window.DataContext>
    <local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
    <local:MyControl Margin="8" MyProperty="{Binding MyPropertyParent}" />
</Grid>

MainWindowViewModel.cs

public class MainWindowViewModel : ObservableObject
{
    public string MyPropertyParent
    {
        get
        {
            return _myProperty;
        }

        set
        {
            _myProperty = value;
            this.OnPropertyChanged();
        }
    }

    private string _myProperty;

    public MainWindowViewModel()
    {
        MyPropertyParent = "IT WORKS!!!";
    }
}

MyControl.xaml

<UserControl.DataContext>
    <local:MyControlViewModel/>
</UserControl.DataContext>
<Border CornerRadius="4" Background="White" BorderThickness="1" BorderBrush="#FFB0AEAE">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition />
        </Grid.RowDefinitions>

        <Border Grid.Row="0" Background="#FFC6C6FB">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <TextBlock Grid.Column="0" Margin="4" FontSize="16" VerticalAlignment="Center" HorizontalAlignment="Left"
                            Text="{Binding MyPropertyHack}" />
            </Grid>
        </Border>
    </Grid>
</Border>

MyControl.xaml.cs

public partial class MyControl : UserControl
{
    public string MyProperty
    {
        get { return (string)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register(
            nameof(MyProperty),
            typeof(string),
            typeof(MyControl),
            new PropertyMetadata(null, MyPropertyPropertyChanged));

    private static void MyPropertyPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
    {
        ((MyControl)o)._viewModel.MyPropertyHack = (string)e.NewValue;
    }

    public MyControlViewModel _viewModel;

    public MyControl()
    {
        InitializeComponent();

        if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            return;

        _viewModel = App.Provider.GetService<MyControlViewModel>();
        this.DataContext = _viewModel;
    }
}

MyControlViewModel.cs

public class MyControlViewModel : ObservableObject
{
    private string _myPropertyHack;
    public string MyPropertyHack
    {
        get
        {
            return _myPropertyHack;
        }

        set
        {
            if (_myPropertyHack != value)
            {
                _myPropertyHack = value;
                this.OnPropertyChanged();
            }
        }
    }

    public MyControlViewModel()
    {

    }
}

我正在尝试获取它,以便 MainWindow.xaml 可以将其视图模型中的属性集传递给 MyControl 的 DependencyProperty。但是,当我运行它时,TextBlock 的文本字段为空,因此“它可以工作!!!” 没有显示在屏幕上,并且我在 MyPropertyPropertyChanged 中的断点没有被命中。

我究竟做错了什么?

标签: c#wpfxaml

解决方案


表达方式

MyProperty="{Binding MyPropertyParent}"

应该创建一个绑定到MyPropertyParent当前 DataContext 中对象的属性。

虽然您似乎认为这是在 MainWindow 中设置的 DataContext 对象,但它实际上是在 UserControl 的构造函数中设置的 DataContext 对象。

您应该已经注意到 Visual Studio 的输出窗口中的数据绑定错误消息,它告诉您MyPropertyParent在类型对象上找不到名为的属性MyControlViewModel

一般来说,控件永远不应该明确设置自己的 DataContext,因为像上面这样的基于 DataContext 的绑定不会按预期工作。

分配给 UserControl 的 DataContext 的视图模型对象对于您的应用程序的其余部分来说是未知的。应用程序将无法与此视图模型进行通信。


推荐阅读