如何绑定到UserControl的财产从ResourceDictionary内?我希望对象我在资源宣布,以具有与DataContext它包含在同一UserControl

<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 Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='UserControl', AncestorLevel='1''. BindingExpression:Path=DataContext; DataItem=null; target element is 'SomeClass' (Name=''); target property is 'DataContext' (type 'Object')

有帮助吗?

解决方案

我的解决方法是设置资源的DataContext在代码隐藏。

<强>的.xaml

<local:SomeType x:Key="SomeKey" SomeProperty="{Binding ... }" />

<强> .xaml.cs

public SomeControl()
{
    InitializeComponent();
    ((SomeType)this.Resources["SomeKey"]).DataContext = this;
}

其他提示

当使用FindAncestor,目标元素必须是派生的源的(逻辑的或者视觉的)。你的对象不出现无论是在视觉或逻辑树,它只是在资源。所以你不能用FindAncestor使用的RelativeSource为对象。

您可以在绑定,虽然使用的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>

设置 X:共用 = “假”,这将在克隆资源每次使用,并让它成为你的子元素,使绑定的使用。

<local:SomeClass
            x:Key="SomeClass"
            x:Shared="False"
            DataContext="{Binding Path=DataContext, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}}" />

我觉得你要寻找的只是{}绑定结合到继承的DataContext的。下面是一个例子,虽然有点怪显示如何通过绑定到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>

我会做的是创建用户控制附加的行为(ContextualizeResourceBehavior),并指定该附加的行为的资源键。该行为将查找的资源(不知道你将能够做到这一点的重视,如果不是你需要挂钩Loaded事件)和传输数据的上下文。

当你把你的资源,以可视化树应该继承的数据上下文。但是...看看元素间谍这可能只是你需要什么。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top