tags:

views:

693

answers:

2

I need to apply some text formatting when displaying items in a WPF ListView. The text formatting is nothing too fancy just setting the background colour differently on certain strings within the text.

The text does not lend itself to different columns so it's not an option to apply a style to one of the ListView columns.

I'm pretty sure the answer is to somehow use Paragraphs or Runs within a TextBlock control in the ListView but I can't find out how to that in the XAML. What I've got so far that demonstrates what I am trying to do but that doesn't work is:

<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>

The ListDescription prpoperty that is being bound to would be a Paragraph or some other container control that can hold formatted text.

A: 

I don't think this is possible in pure XAML. You can format an entire string with the same style with a textblock or a RichTextBlock, but to to apply different formatting to substrings, and you'll need a user control or some other form of code logic - such as a value converter.

Scott Weinstein
A: 

I found the answer on a related question: 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>

And for the data source I bind to the ListView I have:

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