【问题标题】:XAML: Same Template, Different BindingsXAML:相同的模板,不同的绑定
【发布时间】:2013-08-16 11:53:49
【问题描述】:

我正在尝试创建一个带有 GridViewListView,该 GridView 有 2 列:名称和日期,稍后将绑定到 Person Object

我想使用DataTemplate,此模板包含Label

我的问题是我想对两列都使用这个模板,但是标签的内容要绑定到每列中的不同属性。简而言之,我希望能够在GridViewColumn 代码块中而不是DataTemplate 代码块中绑定标签的内容。

提前感谢您的帮助。

【问题讨论】:

  • 我认为展示您的 XAML 布局会有所帮助。
  • 似乎对多列绑定使用相同的DataTemplate 是一个死胡同,除非您使用不同的模板。如果您可以改用 DisplayMemberBinding 属性,那么您可以通过将列绑定设置为 Person 对象的 Name 或 Date 属性来做您想做的事情。
  • @gitsitgo 如果我使用了DisplayMemberBinding,那么我将无法使用我之前创建的DataTemplate。我现在所做的(但不满意)是创建一个带有Binding 到日期的模板,以及另一个带有Binding 到名称的模板。是的,我认为我的问题与您提到的问题重复。我现在应该删除我的问题吗?
  • 如果副本中没有一个答案让您满意,那么...我认为(我在这里也很新)...您可以让这个答案保持打开状态。你有什么理由必须使用DataTemplate?除非您在DataTemplate 中添加更多 个控件,否则DisplayMemberBinding 会有效地做同样的事情,它可以让您在GridViewColumn 中绑定并且应该适用于网格中的一个标签查看项目。

标签: c# wpf xaml binding datatemplate


【解决方案1】:

我在数据模板中使用空绑定尝试了这种方式,这在我的情况下解决了..(没有检查具有多个要绑定的属性的对象类型)。这将适用于这个问题的上下文。

<DataTemplate x:Key="commonTemplate">
    <Label Content="{Binding}" />
</DataTemplate>

<GridViewColumn>
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <ContentPresenter Content="{Binding Name}"
                ContentTemplate="{StaticResource commonTemplate}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

<GridViewColumn>
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <ContentPresenter Content="{Binding Date}"
                ContentTemplate="{StaticResource commonTemplate}"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>

欢迎任何更好的方法或改进..谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 2013-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多