tags:

views:

463

answers:

1

I have some text data that I want to display in a grid, with three columns, the middle column being twice as wide as the other two, taking up the full width of the grid. The text is long and needs to be wrapped. What I can't get to work (and from other queries here in the past, I see others have had similar problems) is getting word wrap and sizing to the grid to work. What I have is:

<Window.Resources>
  <local:DTData x:Key="dtData" />
</Window.Resources>

<StackPanel DataContext="{StaticResource dtData}">
  <ListBox ItemsSource="{Binding}" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="*" />
          </Grid.ColumnDefinitions>
          <Border x:Name="a" Grid.Column="0" Margin="4"/>
          <TextBlock Margin="4" Grid.Column="0" TextWrapping="Wrap" 
                     Text="{Binding A}" Width="{Binding ActualWidth, ElementName=a }" MinWidth="100"/>
          <Border x:Name="b" Grid.Column="1" Margin="4"/>
          <TextBlock Margin="4" Grid.Column="1" TextWrapping="Wrap" 
                     Text="{Binding B}" Width="{Binding ActualWidth, ElementName=b }" MinWidth="100"/>
          <Border x:Name="c" Grid.Column="2" Margin="4"/>
          <TextBlock Margin="4" Grid.Column="2" TextWrapping="Wrap" 
                     Text="{Binding C}" Width="{Binding ActualWidth, ElementName=b }" MinWidth="100"/>
        </Grid>
      </DataTemplate>
    </ListBox.ItemTemplate>
  </ListBox>

This uses the Border trick from http://stackoverflow.com/questions/386039/wpf-textbox-and-scroll-behavior to force text wrapping to work but the column's width is either the minimum width set or the longest word if greater.

Does anyone know of a way to force the columns to fit the width of the grid?

A: 

Hi Amaca.

You sure you wanna wrap the text? Or you goal is text trimming? I'm asking because when I type the following xaml in kaxaml, the text in the middle column is perfectly wrapped:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"&gt;
  <Grid>  
    <StackPanel>
  <ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled" Width="350">
    <ListBox.ItemTemplate>
      <DataTemplate>
        <Grid>
          <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="2*"/>
            <ColumnDefinition Width="*" />
          </Grid.ColumnDefinitions>
          <TextBlock Margin="4" Grid.Column="0" TextWrapping="Wrap" 
                     Text="Binding A"  MinWidth="100"/>
          <TextBlock Margin="4" Grid.Column="1" TextWrapping="Wrap" 
                     Text=" Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco"  MinWidth="100"/>
          <TextBlock Margin="4" Grid.Column="2" TextWrapping="Wrap" 
                     Text="Binding C" MinWidth="100"/>
        </Grid>
      </DataTemplate>
    </ListBox.ItemTemplate>
    <TextBlock Text="" />
    <TextBlock Text=""/>
    <TextBlock Text=""/>
  </ListBox>
  </StackPanel>
  </Grid>
</Page>

If you want to trim the text, just set TextWrapping="NoWrap" and TextTrimming to TextTrimming="CharacterEllipsis" where needed.

There might be also that you haven't provided all the data, to reproduce the problem you are describing...

Anvaka
Thanks, but that meets two out of the three requirements: it wraps correctly and the columns expand to fir the width of the grid; but the column widths are variable with real data - yours align prettily because you're using the same data for each row.What I haven't been able to do is get the text to wrap, the columns to fit the width of the listbox and the column widths to be consistent for each row.
amaca
Could you add a picture showing what you are trying to do? I still can't get it..
Anvaka