【问题标题】:What does it mean: "the ContentControl does not use the DataTemplate automatically."?这是什么意思:“ContentControl 不会自动使用 DataTemplate。”?
【发布时间】:2017-08-25 12:35:36
【问题描述】:

我正在努力让我的数据模板绑定工作;我将我的 DataTemplate 显式绑定到数据类型

<DataTemplate DataType="{x:Type local:ExcelReportVM}">
    <local:ExcelReport DoubleClickHandler="{Binding}">
        <local:ExcelReport.RowColorConverter>
            <local:ReportRowColorConverter/>
        </local:ExcelReport.RowColorConverter>
    </local:ExcelReport>
</DataTemplate>

但是,我发现即使我的control.DataContextExcelReportVM,上面的DataTemplate 也根本不适用。

那我read in MSDN那个

如果您将 ContentControl 绑定到 Task 对象的集合, ContentControl 不会自动使用 DataTemplate。这 是因为 ContentControl 上的绑定需要更多信息 区分您是要绑定到整个集合还是 个别对象。如果您的 ContentControl 正在跟踪选择 ItemsControl 类型的,您可以设置的 Path 属性 ContentControl 绑定到“/”表示你有兴趣 当前项目。例如,请参阅如何:绑定到集合和 根据选择显示信息。否则,您需要指定 通过设置 ContentTemplate 属性显式的 DataTemplate。

这个解释听起来很抽象,我看了几遍都不知道在说什么。有人愿意用适当的例子来解释吗?

【问题讨论】:

  • 什么是“控件”?
  • @mm8 ,数据模板中定义的 datatype ,ExcelReportVM,不是吗?
  • 您设置了一些“控件”的 DataContext 属性。这是什么?
  • @Graviton,如果有 ExcelReportVM 的 DataTemplate 并且视图显示 ExcelReportVM 对象的 ExcelReport 控件,这意味着模板已应用。否则 ExcelReportVM 将显示为带有“ExcelReportVM”文本的 TextBlock

标签: c# wpf datatemplate contentcontrol


【解决方案1】:

如果您将ContentControlContent 属性设置或绑定到ExcelReportVM 对象的实例,则会应用隐式DataTemplate

<ContentControl x:Name="control" />

content.Content = new ExcelReportVM();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-17
    • 2011-09-04
    相关资源
    最近更新 更多