2

所以我有一些类似于以下的代码:(请原谅任何拼写错误——我试图在帖子的 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,但它没有bind 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')

4

2 回答 2

3

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

为了解决这个问题,您可以在包含您正在寻找的绑定中创建一个Freezeable对象,然后将您的绑定到该对象.Resourcesmy: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

于 2012-09-13T16:32:31.577 回答
0

我通过将 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>
于 2018-12-26T14:21:01.267 回答