【问题标题】:DataContext not binding in Style.TriggerDataContext 在 Style.Trigger 中未绑定
【发布时间】:2012-09-06 01:52:25
【问题描述】:

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

【问题讨论】:

  • 您的输出窗口中是否有关于此绑定的任何错误或警告消息?
  • @Miklós:很好的建议,谢谢。呃,我没想到绑定错误出现在输出窗口中!现在更新我的问题与错误...
  • @HolySamosa 你的UserControl 在什么位置?您发布的错误听起来像是在没有DataContext 的对象中,例如DataGridColumn.Header。此外,如果多个对象应用该样式,您真的应该使用ContentTemplate 而不是Content 以避免异常:)
  • @Rachel:用户控件是我创建的 MVVM 视图。此外,我试图清理我的示例代码以省略我的实现细节。我使用的 ContentControl 实际上是来自 MahApps 的处理转换的自定义控件。我正在尝试使用您在回答 Bartek 问题时链接到的文章中的 BindingProxy。我似乎在这方面取得了进展。 (当我试图做一些脑残的事情时,我删除了我之前的评论。:-)
  • @Rachel:是的,Thomas Levesque 的BindingProxy 解决了我的问题。谢谢!如果您想留下带有文章链接的答案,我很乐意为您提供正确答案!

标签: wpf xaml mvvm binding datacontext


【解决方案1】:

您发布的错误听起来像是您的自定义控件位于没有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}}"/>

这是从here 复制的示例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 以避免异常:)

【讨论】:

    【解决方案2】:

    我通过将 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-07
      • 2012-11-22
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2016-04-20
      • 2014-03-09
      • 1970-01-01
      相关资源
      最近更新 更多