【问题标题】:Binding DataGridHeader ContentTemplate in Silverlight在 Silverlight 中绑定 DataGridHeader ContentTemplate
【发布时间】:2011-05-18 18:47:09
【问题描述】:

我知道通过Dynamically setting the Header text of a Silverlight DataGrid Column 将文本绑定到 DataGrid 标头是不可能的,但我在网上遵循一些代码示例来用 TextBlock 代替标头。但是,无论我做什么,我绑定的文本都不会显示。

我的 XML:

<dg:DataGridTextColumn Binding="{Binding Path=UnitKey}" IsReadOnly="True">
 <dg:DataGridTextColumn.HeaderStyle>
  <Style TargetType="dataprimitives:DataGridColumnHeader">
   <Setter Property="ContentTemplate">
    <Setter.Value>
    <DataTemplate>
      <TextBlock Text="{Binding Path=KeyListName}"/>
    </DataTemplate>
    </Setter.Value>
   </Setter>
  </Style>
 </dg:DataGridTextColumn.HeaderStyle>
</dg:DataGridTextColumn>

我的视图模型中的 C#:

    public string KeyListName
    {
        get { return keyListName; }
        set
        {
            keyListName = value;
            raisePropertyChanged("KeyListName");
        }
    }

我无法弄清楚我忽略了什么......提前感谢您的帮助!

【问题讨论】:

  • 尝试使用 DataGridTemplateColumn 而不是 DataGridTextColumn
  • 我最初使用的是 TemplateColumn,但它失去了它的视觉风格。

标签: silverlight mvvm datagrid silverlight-4.0


【解决方案1】:

在您的 TextBlock 中放置一个数据上下文

 <TextBlock DataContext="" Text="{Binding Path=KeyListName}"/>

如果它们与您的 UnitKey 具有相同的数据上下文

<dg:DataGridTextColumn x:Name="dgColumnUnit" Binding="{Binding Path=UnitKey}" IsReadOnly="True">

不绑定的原因是你的DataGridTextColumn的父已经绑定了,尝试调试一下看看你的输出窗口会提示KeyListName不能是找到....

【讨论】:

    猜你喜欢
    • 2019-09-07
    • 2011-11-03
    • 1970-01-01
    • 2016-06-05
    • 2018-11-27
    • 2011-05-14
    • 1970-01-01
    • 2013-02-26
    • 2013-02-18
    相关资源
    最近更新 更多