在DataContext的不Style.Trigger结合(DataContext not bind

2019-09-22 08:44发布

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

<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发生时,二传设置Content属性my:CustomView ,但它绑定DataContext 。 如果我提出相同的代码触发的以外DataContext绑定的工作就好了。

有任何想法吗? 如果这是某些种类的限制,有没有解决办法?

更新:

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

System.Windows.Data Error: 3 : Cannot find element that provides DataContext. BindingExpression:Path=DataContextForMyCustomView; DataItem=null; target element is 'CustomView' (Name='customView'); target property is 'DataContext' (type 'Object')

Answer 1:

您发布的错误会使得它听起来像你的自定义控制是一个对象,它不具有DataContext ,如DataGridColumn.Header

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

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

...

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

下面是一个示例代码Freezable从复制的对象在这里 :

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 ,而不是Content ,以避免异常,如果超过一个对象应用于的风格:)



Answer 2:

我通过将用户控件到资源,然后更改与内容解决了类似的问题。

例如,从我自己的代码(不同的名字,相同的概念)

<ContentControl Grid.Column="1"
                Margin="7,0,7,0">
    <ContentControl.Resources>
        <mapping:Slide11x4MappingView x:Key="Slide11X4MappingView" DataContext="{Binding MappingViewModel}"/>
        <mapping:MicrotubeMappingView x:Key="MicrotubeMappingView"  DataContext="{Binding MappingViewModel}"/>
    </ContentControl.Resources>
    <ContentControl.Style>
        <Style TargetType="{x:Type ContentControl}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Acquirer.Sorter.TrayType}" Value="{x:Static mapping:TrayType.SLIDES11X4}">
                    <Setter Property="Content" Value="{StaticResource Slide11X4MappingView}"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Acquirer.Sorter.TrayType}" Value="{x:Static mapping:TrayType.VIALS}">
                    <Setter Property="Content" Value="{StaticResource MicrotubeMappingView}"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ContentControl.Style>
</ContentControl>


文章来源: DataContext not binding in Style.Trigger