Grid.IsSharedScopeSize incompatible with * columns in WPF Grid

夙愿已清 提交于 2019-12-24 04:42:08

问题


I am using IsSharedSizeScope in an ItemsControl in WPF to keep the same widths for each row.

Unfortunately this isn't compatible with Width='*' columns, which makes the 'A B C' text column spill off the end of the page.

<Border BorderBrush="Red" BorderThickness="1">
    <StackPanel Grid.IsSharedSizeScope="True">

        <Grid HorizontalAlignment="Stretch">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" SharedSizeGroup="G1"/>
                <ColumnDefinition Width="Auto" SharedSizeGroup="G2" />
                <ColumnDefinition Width="*"  SharedSizeGroup="G3" />
            </Grid.ColumnDefinitions>

            <TextBlock Text="Col0" Grid.Column="0" Margin="0,0,5,0"/>
            <TextBlock Text="Col1" Grid.Column="1" Margin="0,0,5,0"/>

            <TextBlock Text="A B C D E F G H I J K L M N O P Q R S T U V W X Y Z" TextWrapping="Wrap" Grid.Column="2"/> 
        </Grid>

    </StackPanel>                   

</Border>

This won't wrap, but if you change Grid.IsSharedScopeSize to false then it does work, but the rows are uneven.

Is there any clever way to achieve this, or will I have to manually set a width on the 'A B C' text column?

(Note: This is actually inside an ItemsControl not shown)


回答1:


I just realized I don't need to set SharedSizeGroup for every column. By not setting it for the * column I can achieve the wrapping I need while maintaining consistent column widths for the other columns.

Unfortunately I don't think this will work for * columns that aren't the last column in the grid but I haven't tried yet.



来源:https://stackoverflow.com/questions/6552800/grid-issharedscopesize-incompatible-with-columns-in-wpf-grid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!