[英]What is the datacontext of the user control?
我有这样的用户控件:
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding NameC}" Width="100" />
<TextBlock Text="{Binding Filename}" />
</StackPanel>
</Grid>
在DP后面的代码中:
public TestUc()
{
InitializeComponent();
DataContext = this;
}
public static readonly DependencyProperty NameCProperty = DependencyProperty.Register(
"NameC", typeof(string), typeof(TestUc), new PropertyMetadata(default(string)));
public string NameC
{
get { return (string) GetValue(NameCProperty); }
set { SetValue(NameCProperty, value); }
}
public static readonly DependencyProperty FilenameProperty = DependencyProperty.Register(
"Filename", typeof (string), typeof (TestUc), new PropertyMetadata(default(string)));
public string Filename
{
get { return (string) GetValue(FilenameProperty); }
set { SetValue(FilenameProperty, value); }
}
现在,在窗口中使用它时,
这工作正常:
<Window x:Class="TestDpOnUc.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:uc="clr-namespace:TestDpOnUc"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<uc:TestUc NameC="name is xxx" Filename="This is filename" />
</Grid>
但这不是:
<Window x:Class="TestDpOnUc.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:uc="clr-namespace:TestDpOnUc"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<uc:TestUc NameC="{Binding Name}" Filename="{Binding FileName}" />
</Grid>
public MainWindow()
{
InitializeComponent();
DataContext = this;
Name = "name is nafsafd";
FileName = "lkjsfdalkf";
}
private string _Name;
public string Name
{
get { return _Name; }
set
{
_Name = value;
OnPropertyChanged();
}
}
private string _FileName;
public string FileName
{
get { return _FileName; }
set
{
_FileName = value;
OnPropertyChanged();
}
}
有人可以解释为什么吗? 为什么用户控件的数据上下文未自动设置为父窗口-主窗口?
这是因为UserControl
构造函数中的这一行DataContext = this
。 您将DataContext
设置为用户控件,这会影响TestUc
和所有子级(包括<uc:TestUc ... />
默认绑定上下文。 所以目前
<uc:TestUc NameC="{Binding Name}" Filename="{Binding FileName}" />
将在UserControl
查找Name
和FileName
属性。 您需要删除该行,但这会破坏用户控件中的绑定。
<TextBlock Text="{Binding NameC}" Width="100" />
<TextBlock Text="{Binding Filename}" />
将在MainWindow
查找NameC
和Filename
。 解决方案是通过UserControl
内部的RelativeSource
或ElementName
绑定来更改每个绑定的绑定上下文
<UserControl ... x:Name="myUserControl">
<Grid>
<StackPanel Orientation="Vertical">
<TextBlock Text="{Binding ElementName=myUserControl, Path=NameC}" Width="100" />
<TextBlock Text="{Binding ElementName=myUserControl, Path=Filename}" />
</StackPanel>
</Grid>
</UserControl>
创建具有DependencyProperties的UserControl时,必须将ElementName或RelativeSource绑定到UserControl中的DP绑定到UserControl中的控件
<TextBlock Text="{Binding ElementName=myUserControl, Path=NameC}" Width="100" />
而且您永远不会设置DataContext。
DataContext = this; <-- do not do that within your UserControl
当您要使用UserControl时,请将其放在View中,然后将实际DataContext / Viewmodel的Properties绑定到UserControl的DependencyProperties 。
<uc:TestUc NameC="{Binding Name}" Filename="{Binding FileName}" />
在执行<uc:TestUc NameC="{Binding Name}" Filename="{Binding FileName}" />
它不查看MainWindow的数据上下文,而是查看UserControl的数据上下文。
因此,您可能想搜索正确的元素并将其绑定。 下面是使用ElementName
的一种方法,即为Window命名,例如MainWindowName
。 或者,您也可以使用相对源来搜索其祖先。
<Window x:Class="TestDpOnUc.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:uc="clr-namespace:TestDpOnUc"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
x:Name="MainWindowName">
<Grid>
<uc:TestUc NameC="{Binding Element=MainWindowName, Path=DataContext.Name}" Filename="{Binding Element=MainWindowName, Path=DataContext.FileName}" />
</Grid>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.