DataContext未在Style.Trigger中绑定



所以我有一些类似于以下的代码:(请原谅任何拼写错误——我试图在So编辑器中简化这篇文章)

<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时,setter都会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')

您发布的错误听起来像您的自定义控件位于没有DataContext的对象中,例如DataGridColumn.Header

为了解决这个问题,您可以在.Resources中创建一个包含您要查找的绑定的Freezeable对象,然后将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来避免异常:)

我将UserControl放入资源中,然后用它更改内容,从而解决了类似的问题。

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

<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>

最新更新