Wpf 从ResourceDictionary中绑定到祖先
如何从Wpf 从ResourceDictionary中绑定到祖先,wpf,xaml,binding,resourcedictionary,Wpf,Xaml,Binding,Resourcedictionary,如何从资源字典中绑定到用户控件的属性?我希望在我的资源中声明的对象与包含在以下内容中的UserControl具有相同的DataContext: <UserControl xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:So
资源字典
中绑定到用户控件的属性?我希望在我的资源中声明的对象与包含在以下内容中的UserControl
具有相同的DataContext
:
<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Some.Namespace"
DataContext="{Binding Path=ViewModel, RelativeSource={RelativeSource Self}}">
<UserControl.Resources>
<local:SomeClass
x:Key="SomeClass"
DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
</UserControl.Resources>
</UserControl>
在运行时,我得到一个错误:
System.Windows.Data错误:4:找不到引用为“RelativeSource FindAncestor,AncestorType='UserControl',AncestorLevel='1'的绑定源。BindingExpression:Path=DataContext;DataItem=null;目标元素是“SomeClass”(名称=“”);目标属性是'DataContext'(类型'Object')
我认为您要查找的只是绑定到继承的DataContext的{Binding}。下面是一个示例,虽然有点奇怪,但它显示了如何通过绑定到DataContext来获取颜色:
<Window x:Class="AncestorBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Window.Resources>
<SolidColorBrush x:Key="MyBrush" Color="Blue" />
</Window.Resources>
<StackPanel>
<Button DataContext="{Binding Source={StaticResource MyBrush}}" Content="My Button">
<Button.Resources>
<Style TargetType="{x:Type Button}">
<Setter Property="Background" Value="{Binding}" />
</Style>
</Button.Resources>
</Button>
</StackPanel>
</Window>
我要做的是在用户控件上创建一个附加行为(ContextureSourceBehavior),并在该附加行为上指定资源键。该行为将查找资源(不确定您是否能够在附加时执行此操作,如果不能,则需要连接已加载的事件)并传输数据上下文。将资源添加到可视化树时,它应继承数据上下文。但是查看一下,它可能只是做您需要的事情。使用FindAncestor时,目标元素需要是源元素的后代(逻辑或可视)。您的对象既不出现在可视树中,也不出现在逻辑树中,它只是出现在参考资料中。因此,您不能将RelativeSource与FindAncestor一起用于对象
不过,您可以在绑定中使用ElementName。像这样的方法应该会奏效:
<UserControl x:Name="userControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Some.Namespace"
DataContext="{Binding Path=ViewModel, RelativeSource={RelativeSource Self}}">
<UserControl.Resources>
<local:SomeClass
x:Key="SomeClass"
DataContext="{Binding Path=DataContext, ElementName=userControl}" />
</UserControl.Resources>
</UserControl>
我的解决方法是在代码隐藏中设置资源的DataContext
.xaml
<local:SomeType x:Key="SomeKey" SomeProperty="{Binding ... }" />
Set=“False”,这将在每次使用时克隆资源,并使其成为元素的子元素,从而启用绑定的使用
<local:SomeClass
x:Key="SomeClass"
x:Shared="False"
DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />
不幸的是,{Binding}不适合我--找不到提供DataContext的元素。BindingExpression:(无路径);DataItem=null;目标元素是“SomeClass”(名称=“”);目标属性为“DataContext”(类型为“Object”)是否尝试根本不设置DataContext?我认为您的local:SomeClass会从UserControl继承DataContext。我最初遇到这个问题是因为我根本没有设置DataContext:直接在代码隐藏中设置DataContext可以工作,但我想要一个XAML解决方案。。。((SomeClass)this.Resources[“SomeClass”]).DataContext=viewmodel;我认为继承上下文可能与这个问题有关。。。这对我不起作用。绑定无法使用x:name或name找到元素名称。感谢您(认真地)回答自己的问题。我从未见过这个问题的XAML唯一解决方案。它还影响ContextMenu、MenuItem和MenuItem.Icon。让我发疯。这似乎违背了XAML的目的。