WrapPanel: Trying to make the ItemWidth equal to the max width of any one element

后端 未结 3 538
情话喂你
情话喂你 2021-01-12 07:01

Hopefully no one else has already asked this question, but I have searched and cannot find any mention. Feel free to point me in the right direction if I missed another que

相关标签:
3条回答
  • 2021-01-12 07:21

    Try the following. The part to pay attention to is the Grid.IsSharedSizeScope="True" on the wrap panel AND the column definition's SharedSizeGroup property.

    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525">
        <Grid>
            <WrapPanel Grid.IsSharedSizeScope="True">
                <WrapPanel.Resources>
                    <Style TargetType="TextBlock">
                        <Setter Property="Padding" Value="2" />
                        <Setter Property="HorizontalAlignment" Value="Center" />
                    </Style>
                </WrapPanel.Resources>
                <WrapPanel.Children>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="abcdefghijklmnopqrstuvwxyz" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="1234567890" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="abcdefghijklmnopqrstuvwxyz" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="1234567890" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="abcdefghijklmnopqrstuvwxyz" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="1234567890" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="abcdefghijklmnopqrstuvwxyz" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="1234567890" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="abcdefghijklmnopqrstuvwxyz" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="1234567890" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="abcdefghijklmnopqrstuvwxyz" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="1234567890" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="abcdefghijklmnopqrstuvwxyz" />
                    </Grid>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition SharedSizeGroup="group1" />
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="1234567890" />
                    </Grid>
                </WrapPanel.Children>
            </WrapPanel>
        </Grid>
    </Window>
    
    0 讨论(0)
  • 2021-01-12 07:28

    You could wrap each item in a Grid, and use the Grid's ShareSizeScope property to make sure all items share the same width.

    For example, on your WrapPanel you would set Grid.IsSharedSizeScope to true

    <WrapPanel Grid.IsSharedSizeScope="True">
    

    Then you'd wrap each item in a single cell Grid that uses the SharedSizeGroup property to tell them that they all share a size

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition SharedSizeGroup="SharedGroup" />
        </Grid.ColumnDefinitions>
        <ContentPresenter />
    </Grid>
    

    If you want an example, a quick Google search gave me this answer which contains a code example.

    I'd recommend performance testing this too if you have a large set of data.

    0 讨论(0)
  • 2021-01-12 07:45

    try setting the itemwidth property to Auto... see here:

    http://msdn.microsoft.com/en-us/library/system.windows.controls.wrappanel.itemwidth(v=vs.110).aspx

    to perform this "width set" programmatically, you could do the following, once the control has been rendered.

    protected override void OnRender(DrawingContext dc)
    {
    
    int largest = 0;
    foreach(UIElement child in this.myWrapPanel.Children)
    {
        if(child.Width>largest)
            largest = child.Width;
    }
    
    this.myWrapPanel.ItemWidth = largest;
    
    
    }
    
    0 讨论(0)
提交回复
热议问题