【问题标题】:Binding to ancestors from within a ResourceDictionary从 ResourceDictionary 中绑定到祖先
【发布时间】:2011-01-05 16:04:05
【问题描述】:

如何从ResourceDictionary 内绑定到UserControl 的属性?我希望我在资源中声明的对象具有与它包含的 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 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')

【问题讨论】:

标签: wpf xaml binding resourcedictionary


【解决方案1】:

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

.xaml

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

.xaml.cs

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

【讨论】:

  • 感谢您(认真地)回答您自己的问题。我从未见过针对此问题的仅 XAML 解决方案。它还会影响 ContextMenu、MenuItem 和 MenuItem.Icon。让我发狂。这似乎违背了 XAML 的目的。
【解决方案2】:

使用 FindAncestor 时,目标元素需要是源的后代(逻辑或视觉)。您的对象不会出现在视觉树或逻辑树中,它只是在资源中。因此,您不能将 RelativeSource 与 FindAncestor 一起用于您的对象。

您可以在 Binding 中使用 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:Name 或 Name 找到元素名称。
【解决方案3】:

设置x:Shared="False",这将在每次使用时克隆资源并使其成为您元素的子元素,从而可以使用绑定。

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

【讨论】:

    【解决方案4】:

    我认为您正在寻找的只是绑定到继承的 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>
    

    【讨论】:

    • 不幸的是 {Binding} 对我不起作用--找不到提供 DataContext 的元素。 BindingExpression:(无路径);数据项=空;目标元素是 'SomeClass' (Name='');目标属性是“DataContext”(类型“对象”)
    • 您是否尝试过完全不设置 DataContext?我认为您的 local:SomeClass 会从 UserControl 继承 DataContext。
    • 我最初遇到这个问题是因为我根本没有设置DataContext:stackoverflow.com/questions/2073170
    【解决方案5】:

    我要做的是在用户控件上创建一个附加行为 (ContextualizeResourceBehavior),并在该附加行为上指定资源键。该行为将查找资源(不确定您是否能够在附加时执行此操作,否则您需要连接 Loaded 事件)并传输数据上下文。

    【讨论】:

      【解决方案6】:

      当您将资源添加到可视化树时,它应该继承数据上下文。但是...看看element spy,它可能会满足您的需求。

      【讨论】:

        猜你喜欢
        • 2011-03-25
        • 2011-10-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多