Alexandr Sulimov
Alexandr Sulimov

Reputation: 1924

XAML Strech TextBlock in Grid Colum

enter image description here

Each list item must be

  1. TextBlock 1 fill first row with 100% width;

  2. TextBlock 2,3,4 must fill 33% each on separate row;

Why TextBlock 2,3,4 not strech?

<ListView.ItemTemplate><DataTemplate><StackPanel>
    <TextBlock Text="{Binding Name}" />
    <Grid HorizontalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <TextBlock TextAlignment="Right" Grid.Row="0" Grid.Column="0" 
             HorizontalAlignment="Stretch" VerticalAlignment="Center" 
             Text="{Binding Rest}" FontSize="28"/>
        <TextBlock TextAlignment="Right" Grid.Row="0" Grid.Column="1" 
             HorizontalAlignment="Stretch" VerticalAlignment="Center" 
             Text="{Binding Currency.Name}" FontSize="25"/>
        <TextBlock TextAlignment="Right" Grid.Row="0" Grid.Column="2" 
             HorizontalAlignment="Stretch" VerticalAlignment="Center" 
             Text="{Binding FullRest}" FontSize="22"/>
    </Grid>
</StackPanel></DataTemplate></ListView.ItemTemplate>

P.S.

How i can add 1...x rows in list view in design time?

Upvotes: 0

Views: 471

Answers (3)

Anton Sulimov
Anton Sulimov

Reputation: 61

Use ListView.ItemContainerStyle

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
        <Setter Property="HorizontalContentAlignment" Value="Stretch" />
    </Style>
</ListView.ItemContainerStyle>

Upvotes: 1

Raunaq Patel
Raunaq Patel

Reputation: 280

I used a ListBox to bind my data in similar way.

You will need to give stackpanel specific width

And you will have to set textwrapping property to 'no wrap'

<ListBox x:Name="listbox" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Width="480">
                        <TextBlock Text="{Binding main}" />
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock TextWrapping="NoWrap" Text="{Binding one}"/>
                            <TextBlock TextWrapping="NoWrap" Text="{Binding two}" Grid.Column="1"/>
                        <TextBlock TextWrapping="NoWrap" Text="{Binding thr}" Grid.Column="2"/>
                    </Grid>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
        </ListBox>

This Works perfectly for me!

Upvotes: 1

Niels Uytdenhouwen
Niels Uytdenhouwen

Reputation: 105

This code (with normal values, because I don't have the underlying data structure) works fine for me.

Different rows can have different widths however. That can be fixed by setting the Width of the StackPanel to the ActualWidth - margins of the listview.

You can find some info about how to add mock data, for usage in the designer, here: How to get mock data into listview during design time and real data at run time in WPF

Upvotes: 1

Related Questions