c# - WPF c#相对源祖先数据绑定
问题描述
我在内容控件中有一个带有用户控件的主窗口
<MainWindow Title="MainWindow">
<!-- MainWindows DataContext is set to MainWindowViewModel -->
<ContentControl Content="{Binding CurrentViewModel}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type viewmodels:ViewModel1}">
<local:UserControl1/>
</DataTemplate>
</ContentControl.Resources>
<ContentControl/>
<MainWindow/>
和一个带有文本框的用户控件
<UserControl Title="UserControl1">
<TextBlock Text="{Binding Path=Words}"/>
<UserControl/>
和一个 MainWindowViewModel
public class MainWindowViewModel
{
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel
{
get { return _currentViewModel; }
set
{
_currentViewModel = value;
OnPropertyChanged("CurrentViewModel");
}
}
private string _words;
public string Words
{
get { return _words; }
set
{
_words = value;
OnPropertyChanged("Words");
}
}
}
如何将文本块文本绑定到 Words 属性?我试过这个,但它不工作......
<TextBlock Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContentControl}}, Path=DataContext.Words}" />
解决方案
推荐阅读
- excel - VBA复制粘贴导致形状相乘
- graphics - 细发光线模糊且碎裂
- python - 由于溢出,无法读取 botocore.response.StreamingBody
- python - 使用具有 2 种自定义颜色的 Matplotlib 在散点图中设置正值和负值的颜色
- asp.net-mvc - How to get Tags, Title, Subject Manager etc from powerpoint (or other) files in .net
- regex - 信用卡号码和详细信息的正则表达式
- ansible - 在 ansible 中找到的目录上添加粘性位
- java - Moving more than one node simultaneously in contiki-os (Mobility)
- ethereum - Web3 getTransaction returns null even in geth same
- storage - AnyLogic:将两个代理同时移动到托盘架中