WPF列表视图文本列,有些行下划线,有些没有

本文关键字:下划线 列表 视图 WPF 文本 | 更新日期: 2023-09-27 18:16:52

我需要在列表视图中显示绑定数据,以便同一列的一些单元格加下划线,而其他单元格不加下划线:

如何创建将在同一单元格中显示不同控件类型的数据模板?(Visual Studio 2013, .NET 4.5.2)

XAML:

<ListView Grid.Column="3" ItemsSource="{Binding Results.MyItemSource, Mode=OneWay}">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Width="Auto" Header=" " DisplayMemberBinding="{Binding Path=Caption, Mode=OneWay}"/>
                <GridViewColumn Width="Auto" Header=" " DisplayMemberBinding="{Binding Path=Value, Mode=OneWay}" >
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

ItemSource:

public List<object> MyItemSource
{
   get
   {
      return new List<object>
      {
         new CaptionValuePair {Caption = "Caption1", Value = new TextBlock {Text = "Value1"}},
         new CaptionValuePair {Caption = "Caption2", Value = new TextBlock {Text = "Value2", TextDecorations = TextDecorations.Underline}}
      };
   }
}

绑定类型:

public class CaptionValuePair
{
   public string Caption { get; set; }
   public object Value { get; set; }
}

问题:StackPanel只是显示文本块的类型名。

WPF列表视图文本列,有些行下划线,有些没有

代替StackPanel,使用ItemsControl并绑定它的ItemsSource属性。

XAML:

<GridViewColumn.CellTemplate>
   <DataTemplate>
      <ItemsControl ItemsSource="{Binding Path=Value, Mode=OneWay}" />
   </DataTemplate>
</GridViewColumn.CellTemplate>

绑定类型:

public List<UIElement> Value{ get; set; }