c# – 我的WPF自定义控件的数据上下文正在取代父级
|
在我的主窗口中,我尝试绑定到一个布尔.但它正在查看我的自定义控件的数据上下文.如果我没有在用户控件中分配DataContext,那么主窗口的绑定工作,但(显然)这会在用户控件中制动绑定.这是错误:
System.Windows.Data Error: 40 : BindingExpression path error: 'MyControlVisible' property not found on 'object' ''MyUserControlModel' (HashCode=1453241)'. BindingExpression:Path=MyControlVisible; DataItem='MyUserControlModel' (HashCode=1453241); target element is 'MyUserControl' (Name='_myUserControl'); target property is 'Visibility' (type 'Visibility') 我需要绑定才能在两个控件上工作,但我不希望用户控件的datacontext取代窗口. 这是代码: <Window x:Class="Sandbox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:Sandbox.Controls" Title="Sandbox">
<DockPanel LastChildFill="True">
<DockPanel.Resources>
<BooleanToVisibilityConverter x:Key="boolToVis" />
</DockPanel.Resources>
<Grid>
<Controls:MyUserControl x:Name="_myUserControl" Visibility="{Binding MyControlVisible,Converter={StaticResource boolToVis}}"/>
</Grid>
</DockPanel>
</Window>
namespace Sandbox
{
public partial class MainWindow
{
private MainWindowModel model;
public MainWindow()
{
InitializeComponent();
DataContext = model = new MainWindowModel();
_myUserControl.Initialize(model.MyUControlModel);
}
}
}
using System.ComponentModel;
using Sandbox.Controls;
namespace Sandbox
{
public class MainWindowModel : BaseModel
{
public MyUserControlModel MyUControlModel { get; set; }
public bool MyControlVisible { get; set; }
public MainWindowModel()
{
MyUControlModel = new MyUserControlModel();
MyControlVisible = false;
OnChange("");
}
}
public class BaseModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnChange(string s)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this,new PropertyChangedEventArgs(s));
}
}
}
}
<UserControl x:Class="Sandbox.Controls.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"
mc:Ignorable="d">
<Grid>
<TextBlock Text="{Binding MyBoundText}"/>
</Grid>
</UserControl>
namespace Sandbox.Controls
{
public partial class MyUserControl
{
public MyUserControl()
{
InitializeComponent();
}
public void Initialize(MyUserControlModel context)
{
DataContext = context;
}
}
}
namespace Sandbox.Controls
{
public class MyUserControlModel : BaseModel
{
public string MyBoundText { get; set; }
public MyUserControlModel()
{
MyBoundText = "Hello World!";
OnChange("");
}
}
}
解决方法
这是您不应该直接从UserControl本身设置DataContext的众多原因之一.
执行此操作时,您不能再使用任何其他DataContext,因为UserControl的DataContext是硬编码的. 在绑定的情况下,通常会继承DataContext,因此Visibility绑定可以在当前DataContext上找到属性MyControlVisible,但是因为您在UserControl的构造函数中对DataContext进行了硬编码,所以找不到该属性. 您可以在绑定中指定不同的绑定源,例如 <Controls:MyUserControl Visibility="{Binding
RelativeSource={RelativeSource AncestorType={x:Type Window}},Path=DataContext.MyControlVisible,Converter={StaticResource boolToVis}}" ... />
然而,这只是针对此特定情况的问题的解决方法,并且在我看来并不是永久的解决方案.更好的解决方案是不要在UserControl中对DataContext进行硬编码 根据您的UserControl的用途以及应用程序的设计方式,您可以采用几种不同的方法. >您可以在UserControl上创建一个DependencyProperty以传入值,并绑定到该值. <Controls:MyUserControl UcModel="{Binding MyUControlModelProperty}" ... />
和 <UserControl x:Class="Sandbox.Controls.MyUserControl"
ElementName=MyUserControl...>
<Grid DataContext="{Binding UCModel,ElementName=MyUserControl}">
<TextBlock Text="{Binding MyBoundText}"/>
</Grid>
</UserControl>
>或者您可以构建UserControl,期望在DataContext中将特定属性传递给它.这通常就是我与DataTemplates的结合. <Controls:MyUserControl DataContext="{Binding MyUControlModelProperty}" ... />
和 <UserControl x:Class="Sandbox.Controls.MyUserControl"...>
<Grid>
<TextBlock Text="{Binding MyBoundText}"/>
</Grid>
</UserControl>
>正如我上面所说,我喜欢使用DataTemplates来显示我的UserControls,它们期望DataContext的特定类型的Model,所以通常我的主窗口的XAML看起来像这样: <DataTemplate DataType="{x:Type local:MyUControlModel}">
<Controls:MyUserControl />
</DataTemplate>
<ContentPresenter Content="{Binding MyUControlModelProperty}" ... />
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
