开发者

How can I make my Grid Columns always be the same width?

If I set the Column's width to *, they're the same width initially but if an item is larger than the amount allowed then it will stretch the column width.

How can I force my Grid to keep it's columns the same size with explicitly defining a size?

I cannot use a UniformGrid because this Grid is being used in an ItemsControl, and the Items need to be placed in specific Grid.Row/Grid.Column spots

Edit Here's a sample of my current code.

<DockPanel>

    <!-- Not showing code here for simplicity -->
    <local:ColumnHeaderControl DockPanel.Dock="Top" />
    <local:RowHeaderControl DockPanel.Dock="Left" />

    <ItemsControl ItemsSource="{Binding Events}">
        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Grid.Column" 
                        Value="{Binding DueDate.DayOfWeek, 
                            Converter={StaticResource EnumToIntConverter}}" />
            </Style>
        </ItemsControl.ItemContainerStyle>

        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="*" />
                    </Grid.ColumnDefinitions>
                </Grid>
            </ItemsPanelTemplate>
        </ItemsPanel>
    </ItemsControl>

</DockPanel>

Edit #2 Here's my final solution. It makes the columns the correct size, and it keeps the size correct when the application gets resized.

<ColumnDefinition Width="{Binding 
    ElementName=RootControl, 
    Path=ActualWidth, 
    Converter={StaticResource MathConverter}, 
    ConverterParameter=(@VALUE-150)/7}" />

150 is the width of the Row Headers + all margins and borders. I'm actually in the process of updating my MathConverter to an IMultiVal开发者_C百科ueConverter so I can bind both parameters (If you're interested in the Converter code it can be found here, although it's only the single-value converter)


You could:

1) Hardcode a size in DIP:

<ColumnDefinition Width="100" />
<ColumnDefinition Width="100" />
...

2) Use SharedSizeGroup, it takes a char

<ColumnDefinition SharedSizeGroup="A" />
<ColumnDefinition SharedSizeGroup="A" />
...

You can read more about it here


You could try binding the width of your columns to a property that divides the total width of the window by the number of columns


The cleanest way is to use a UniformGrid like this:

<UniformGrid Rows="1">
    <Rectangle Fill="Blue" />
    <Rectangle Fill="Yellow" />
    <Rectangle Fill="Red" />
</UniformGrid>

Extra nice when used as ItemsPanel.


Try the IsSharedSizeScope working of the Grid:

<StackPanel Margin="15" Grid.IsSharedSizeScope="True">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto" SharedSizeGroup="B"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Column="0" Text="Col 1"/>
        <TextBox Grid.Column="1" />
        <TextBlock Grid.Column="2" Text="3rd column here"/>
    </Grid>

    <Separator Margin="0,20"/>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" SharedSizeGroup="A"/>
            <ColumnDefinition />
            <ColumnDefinition SharedSizeGroup="B"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Column="0" Text="1"/>
        <TextBox Grid.Column="1"/>
    </Grid>
</StackPanel>

I hope it helps.

For the detail description check this link: https://wpf.2000things.com/tag/sharedsizegroup/


I have not tried it, but I think this should work:

XAML:

<ColumnDefinition Width="*" Loaded="ColumnDefinition_Loaded"/>

C#:

private void ColumnDefinition_Loaded(object sender, RoutedEventArgs e)
        {
            ((ColumnDefinition)sender).MaxWidth = ((ColumnDefinition)sender).ActualWidth;
        }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜