简体   繁体   English

绑定到父DataContext的资源

[英]Resource with binding to parent DataContext

I want to have access to parent DataContext in resource to use it in binding. 我想访问资源中的父DataContext以便在绑定中使用它。 Here is sample: 这是示例:

<Window x:Class="WpfApplication44.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:l="clr-namespace:WpfApplication44"
    x:Name="MyWindows"
    Title="MainWindow"
    Width="525"
    Height="350"
    DataContext="{Binding RelativeSource={RelativeSource Self}}">

<Window.Resources>
    <l:SomeResource x:Key="SomeResource">
        <l:SomeResource.Context>
            <!--
                DataContext is set to windows object.
                I want to bind to window`s title property
            -->
            <Binding Path="Title" />
        </l:SomeResource.Context>
    </l:SomeResource>
</Window.Resources>

<StackPanel>
    <Label>
        <StaticResource ResourceKey="SomeResource" />
    </Label>

    <!--  UPD  -->
    <TextBlock Text="{Binding Source={StaticResource SomeResource}, Path=Context}" />
</StackPanel>

But I get: 但是我得到:

System.Windows.Data Error: 2 : Cannot find governing FrameworkElement or FrameworkContentElement for target element. System.Windows.Data错误:2:找不到目标元素的管理FrameworkElement或FrameworkContentElement。 BindingExpression:Path=Title; BindingExpression:Path =标题; DataItem=null; DataItem = null; target element is 'SomeResource' (HashCode=25557385); 目标元素是'SomeResource'(HashCode = 25557385); target property is 'Context' (type 'Object') 目标属性是“上下文”(类型“对象”)

SomeResource is derived from DependencyObject and contains only one dependency property Context of type object. SomeResource从DependencyObject派生,仅包含一个object类型的Context属性。

It looks like resources don't have access to parent's DataContext property and it is not set event if resource is of type FrameworkElement. 看起来资源没有访问父对象的DataContext属性的权限,并且如果资源的类型为FrameworkElement,则不会设置事件。 I've tried to use ElementName, RelativeSource in my binding but with no luck. 我尝试在绑定中使用ElementName,RelativeSource,但是没有运气。

All I need is to set parent's DataContext to resources. 我需要做的就是将父级的DataContext设置为资源。 I'm using MVVM so any MVVM solutions are preferable. 我正在使用MVVM,因此最好使用任何MVVM解决方案。

UPD Link to project is here UPD链接到项目在这里

Oh it looks like a DataProxy than modify your SomeResource to Freezable like this: 哦,看起来像是DataProxy,而不是像这样将您的SomeResource修改为Freezable

public class SomeResource : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new SomeResource();
    }

    public static readonly DependencyProperty ContextProperty = DependencyProperty.Register("Context", typeof(object), typeof(SomeResource), new PropertyMetadata(default(object)));

    public object Context
    {
        get { return (object)GetValue(ContextProperty); }
        set { SetValue(ContextProperty, value); }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM