c# - 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 中的断点没有被命中。
我究竟做错了什么?
解决方案
表达方式
MyProperty="{Binding MyPropertyParent}"
应该创建一个绑定到MyPropertyParent
当前 DataContext 中对象的属性。
虽然您似乎认为这是在 MainWindow 中设置的 DataContext 对象,但它实际上是在 UserControl 的构造函数中设置的 DataContext 对象。
您应该已经注意到 Visual Studio 的输出窗口中的数据绑定错误消息,它告诉您MyPropertyParent
在类型对象上找不到名为的属性MyControlViewModel
。
一般来说,控件永远不应该明确设置自己的 DataContext,因为像上面这样的基于 DataContext 的绑定不会按预期工作。
分配给 UserControl 的 DataContext 的视图模型对象对于您的应用程序的其余部分来说是未知的。应用程序将无法与此视图模型进行通信。
推荐阅读
- c# - 具有现有 Android SDK 配置的现有 Visual Studio 2015
- python - 我对以下代码有问题 PYTHON nameerror: name 'self' is not defined
- python - Scrapy - 在两个变量之间拆分选择器部分
- java - 不确定我的字符串代码是否区分大小写
- python - 如何在python的不同目录中提取zip文件?
- javascript - React Material-UI 复选框 onChange 事件不会触发
- javascript - 深度合并两个json对象es6
- google-bigquery - BigQuery 在 2019 年 10 月 29 日中断?
- python - 如何在多个命名输入估计器模型上使用 Tensorflow Serving RESTful API?
- r - 理解 [.... with=F][[1]] 的含义