简体   繁体   中英

C# WPF columns' alignment inside a ListView

In my application I have a ListView where I defined its ItemTemplate as follows:

<ListView>
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid HorizontalAlignment="Stretch" Background="Aqua">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" SharedSizeGroup="ParameterIconSize" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="ParameterDescriptionSize" />
                    <ColumnDefinition Width="Auto" SharedSizeGroup="ParameterValueSize" />
                </Grid.ColumnDefinitions>
                <Image
                    Grid.Column="0"
                    MaxHeight="50"
                    Margin="5"
                    Grid.IsSharedSizeScope="True"
                    Source="{Binding Path=Icon}" />
                <TextBlock
                    Grid.Column="1"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Center"
                    Grid.IsSharedSizeScope="True"
                    Text="{Binding Path=Parameter.NameKey}" />
                <TextBox
                    Grid.Column="2"
                    HorizontalAlignment="Stretch"
                    VerticalAlignment="Stretch"
                    VerticalContentAlignment="Center"
                    Grid.IsSharedSizeScope="True"
                    Text="{Binding Path=Parameter.Value}" />
            </Grid>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Such code produces this output:

代码输出

As you can see, nearly every column has a different width, whilst I wanted to make them even. I thought that going with SharedSizeGroup was the right way, but clearly there is something I am missing...

(PS: I had to set the maxHeight in the Image element because if not this would show:

黄色的是 50x50pixel 位图,问号是 120x120 位图

)

I would like to have all three columns the same size, determined by these criteria:

  • column 0: largest image
  • column 1: largest textbox text
  • column 2: fill the remainig space (as this is also a user input field, and I cannot know a priori if that input will be something like 0 or 1438233482379472 )

EDIT 2: I forgot to specify that I want the textboxes in column 2 to be the same size, filling the available space.

You have to enable the SharedSize functionality in the parent element.

<ListView Grid.IsSharedSizeScope="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            ...
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM