【问题标题】:DataGridRow header binding with StringFormatDataGridRow 标头与 StringFormat 绑定
【发布时间】:2018-08-13 16:57:07
【问题描述】:

tl;dr 我有一个 DataGrid,我正在绑定行标题。但是,我无法让它与绑定上的 StringFormat 属性一起使用。

我有一个这样设置的 WPF DataGrid:

<DataGrid HeadersVisibility="All"
          ItemsSource="{Binding Data}">
    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="Header" Value="{Binding Lane, StringFormat=Lane {0:0}}"/>
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Value1}" Header="Value 1" />
        <DataGridTextColumn Binding="{Binding Value2}" Header="Value 2" />
        <DataGridTextColumn Binding="{Binding Value3}" Header="Value 3" />
        <DataGridTextColumn Binding="{Binding Value4}" Header="Value 4" />
    </DataGrid.Columns>
</DataGrid>

但无论我做什么,我都无法让StringFormat 属性在DataGridRow 标头上正常工作。它显示的只是我绑定的数字,而不是格式文本。但是,如果我将相同的格式字符串放在TextBlock 上,它会完美运行。

<TextBlock Text="{Binding Lane, StringFormat=Lane {0:0}}"/>

有谁知道为什么StringFormat 属性没有正确使用?有没有办法获得我想要的行为?


编辑:这就是 Lane 属性的样子。

public int Lane {
    get { return lane; }
    set {
        lane = value;
        NotifyPropertyChanged();
    }
}

【问题讨论】:

  • 您尝试过 StringFormat={} Lane {0:0} 吗?
  • @Marsh 也不起作用 :(
  • 什么是Lane
  • 这是一个整数属性@NetMage

标签: c# wpf wpfdatagrid


【解决方案1】:

我找到了答案。这与Header 属性是一个对象而不是字符串这一事实有关,因此不使用StringFormat 属性(有关更多信息,请参阅this question)。为了解决这个问题,我需要为该行设置数据模板。下面的风格达到了我想要的。

<DataGrid.RowStyle>
    <Style TargetType="{x:Type DataGridRow}">
        <Setter Property="Header" Value="{Binding Lane}"/>
        <Setter Property="HeaderTemplate">
            <Setter.Value>
                <DataTemplate DataType="{x:Type sys:String}">
                    <TextBlock Text="{Binding StringFormat=Lane {0:0}}"/>
                </DataTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.RowStyle>

附:向 Marsh 大喊建议设置控件模板,这让我查看了默认控件模板,如果没有它,我在 Google 上的尝试将毫无结果。


编辑:另一种处理方法是使用DataGridRowHeader 上的ContentStringFormat 属性。所以,保持头部绑定在RowStyle,并添加下面的RowHeaderStyle

<DataGrid.RowHeaderStyle>
    <Style TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="ContentStringFormat" Value="Lane {0:0}"/>
    </Style>
</DataGrid.RowHeaderStyle>

【讨论】:

  • 链接中引用的ContentStringFormat 不起作用?
  • @NetMage 据我所知,这不是 DataGridRow 的属性......但它是 DataGridRowHeader 上的属性。谢谢
【解决方案2】:

我做了一个小测试项目,这对我有用。

如前所述,控件模板将覆盖样式。

<DataGrid.RowHeaderStyle>
    <Style TargetType="{x:Type DataGridRowHeader}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <TextBlock Text="{Binding Lane, StringFormat=Lane {0:0}}"/>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.RowHeaderStyle>

对 ContentTemplate 使用数据模板并绑定到正确的源将保留样式。

<Style TargetType="{x:Type DataGridRowHeader}">
    <Setter Property="ContentTemplate">
        <Setter.Value>
            <DataTemplate>
                <TextBlock Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=DataGridRow}, Path=Item.Lane, StringFormat=Lane {0:0}}"/>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

【讨论】:

  • 为了简单起见,我可能会选择我的一个解决方案,但这是另一种处理它的好方法。谢谢。
猜你喜欢
  • 2011-08-17
  • 1970-01-01
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 2016-04-15
  • 2011-05-22
  • 1970-01-01
  • 2013-09-02
相关资源
最近更新 更多