在DataContext的不Style.Trigger结合 [英] DataContext not binding in Style.Trigger

查看:206
本文介绍了在DataContext的不Style.Trigger结合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有类似下面的一些code:(原谅我试图在SO编辑一职,以简化任何typos--)

So I have some code similar to the following: (Forgive any typos-- I tried to simplify in the SO editor for the post)

<my:CustomContentControl>
        <my:CustomContentControl.Style>
            <Style TargetType="{x:Type my:CustomContentControl}">
                <Style.Triggers>                        
                    <DataTrigger Binding="{Binding Path=CurrentView}" Value="MyCustomView">
                        <Setter Property="Content">
                            <Setter.Value>
                                <my:CustomView DataContext="{Binding DataContextForMyCustomView"/>
                            </Setter.Value>
                        </Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </m:CustomContentControl.Style>
</my:CustomContentControl>

问题是,每当 DataTrigger 时,二传手的确实设置内容属性我:CustomView ,但它的绑定的DataContext 。如果我移动相同code触发外的的DataContext 结合的作品就好了。

The problem is that whenever the DataTrigger occurs, the setter does set the Content property to my:CustomView, but it does not bind DataContext. If I move the same code outside of the trigger the DataContext binding works just fine.

任何想法?如果这是某些种类的限制,有没有变通?

Any ideas? If this is a limitation of some sorts, is there any work around?

更新:

我在输出窗口收到以下错误:

I received the following error in the output window:

System.Windows.Data错误:3:找不到元素提供的DataContext。 BindingEx pression:路径= DataContextForMyCustomView;的DataItem = NULL;目标元素是'CustomView'(名称='customView');目标属性的DataContext(类型'对象')

推荐答案

在发布的错误使得它听起来像你的自定义控制在一个对象不具有<​​code>的DataContext ,如 DataGridColumn.Header

The error you posted makes it sound like your custom control is in an object that doesn't have a DataContext, such as a DataGridColumn.Header.

要解决这个问题,你可以创建一个 Freezeable 对象的的.resources 包含绑定你正在寻找,然后绑定你的我:CustomView.DataContext 到该对象

To get around that, you can create a Freezeable object in your .Resources containing the binding you're looking for, then bind your my:CustomView.DataContext to that object

<my:CustomContentControl.Resources>
    <local:BindingProxy x:Key="proxy" 
        Data="{Binding DataContextForMyCustomView, ElementName=MyControl}" />
</my:CustomContentControl.Resources>

...

<my:CustomView DataContext="{Binding Source={StaticResource proxy}}"/>

这里的code从<复制的可冻结对象的样本href=\"http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/\"相对=nofollow>这里

Here's the code for a sample Freezable object copied from here:

public class BindingProxy : Freezable
{
    #region Overrides of Freezable

    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }

    #endregion

    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Data.  
    // This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), 
            typeof(BindingProxy), new UIPropertyMetadata(null));
}

此外,你真的应该使用的ContentTemplate 而不是内容来避免异常,如果多个对象应用于风格:)

Also, you really should use ContentTemplate instead of Content to avoid an exception if more than one object applies that style :)

这篇关于在DataContext的不Style.Trigger结合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆