【问题标题】:WPF Text Formatting in ListViewListView 中的 WPF 文本格式
【发布时间】:2009-03-10 00:50:32
【问题描述】:

在 WPF ListView 中显示项目时,我需要应用一些文本格式。文本格式只是在文本中的某些字符串上设置不同的背景颜色而已。

文本不适合不同的列,因此不能将样式应用于 ListView 列之一。

我很确定答案是在 ListView 的 TextBlock 控件中以某种方式使用 Paragraphs 或 Runs,但我无法在 XAML 中找到如何使用它。到目前为止,我所得到的证明我正在尝试做但不起作用的是:

<ListView>
  <ListView.View>
    <GridView>
      <GridViewColumn Width="600">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <TextBlock>
              <Paragraph>
                <Binding Path="ListDescription" />
              </Paragraph>
            </TextBlock>
          </DataTemplate>
         </GridViewColumn.CellTemplate>
      </GridViewColumn>
    </GridView>
  </ListView.View>
</ListView>

要绑定的 ListDescription 属性是 Paragraph 或其他可以保存格式化文本的容器控件。

【问题讨论】:

    标签: c# wpf listview


    【解决方案1】:

    我认为这在纯 XAML 中是不可能的。您可以使用文本块或 RichTextBlock 以相同的样式格式化整个字符串,但要对子字符串应用不同的格式,您将需要用户控件或其他形式的代码逻辑 - 例如值转换器。

    【讨论】:

      【解决方案2】:

      我找到了一个相关问题的答案:How would I databind a Paragraph to a TextBlock?。

      <GridViewColumn Width="600">
        <GridViewColumn.CellTemplate>
          <DataTemplate>
            <ItemsControl ItemsSource="{Binding ListDescription}">
              <ItemsControl.ItemsPanel>                   
                 <ItemsPanelTemplate> 
                   <WrapPanel/>                    
                 </ItemsPanelTemplate>
              </ItemsControl.ItemsPanel>  
            </ItemsControl>
          </DataTemplate>
        </GridViewColumn.CellTemplate>
      </GridViewColumn>
      

      对于我绑定到我拥有的 ListView 的数据源:

      public InlineCollection ListDescription
      {
          get { return GetFormattedHistoryStatement().Inlines; }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-14
        • 1970-01-01
        • 1970-01-01
        • 2016-09-20
        相关资源
        最近更新 更多