【问题标题】:WPF ListView Column of Text Some Rows Underlined Some NotWPF ListView 文本列 一些行有下划线 一些没有
【发布时间】:2015-10-08 17:20:35
【问题描述】:

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

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

物品来源:

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 只显示文本块的类型名。

【问题讨论】:

  • 我认为您的第二列无法显示任何内容。 StackPanel 是一个空容器。它怎么会显示一些文本?它应该包含诸如TextBlockContentPresenter 之类的内容...您应该更新您的代码以准确反映您所拥有的内容。
  • @Hopeless:我开始尝试使用 TextBlock 而不是 StackPanel,但绑定的对象为 Value 属性提供了 UIElement,我发现 TextBlock 仅呈现绑定的 UIElement 的类型,而不是内容。所以我希望 StackPanel 可以完成这项工作。无论如何,我有一个可行的解决方案(见下面的帖子)。

标签: c# wpf xaml listview


【解决方案1】:

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

XAML:

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

绑定类型:

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

【讨论】:

    猜你喜欢
    • 2014-10-14
    • 2011-05-16
    • 2016-03-30
    • 1970-01-01
    • 2011-08-01
    • 2016-05-19
    • 1970-01-01
    • 2011-04-22
    • 1970-01-01
    相关资源
    最近更新 更多