WPF ListView文本列有些行下划线但有些行没有

时间:2015-10-08 17:20:35

标签: c# wpf xaml listview

我需要在listview中显示数据绑定数据,以便同一列的某些单元格带下划线而其他单元格不是:

如何创建在同一单元格中显示不同控件类型的数据模板? (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只显示文本块的类型名称。

1 个答案:

答案 0 :(得分:0)

使用ItemsControl并绑定其ItemsSource属性而不是StackPanel。

XAML:

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

绑定类型:

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