简体   繁体   English

如何为 WPF 网格中的所有元素设置特定的高度/宽度?

[英]How to set a specific height/width for all elements in a WPF grid?

I have a custom ListBox where each item has a Grid defined where I display boxes.我有一个自定义 ListBox,其中每个项目都有一个 Grid 定义在我显示框的位置。 I can set the widths to be the same length by using the SharedSizeGroup property.我可以使用 SharedSizeGroup 属性将宽度设置为相同的长度。 However, how do I share this property for all these items within the parent ListBox element?但是,如何为父 ListBox 元素中的所有这些项目共享此属性?

I tried setting the property on the ListBox element also but this did not work.我也尝试在 ListBox 元素上设置属性,但这不起作用。

<ListBox
    ItemContainerStyle="{StaticResource MyStyle}"...>

<Style x:Key="MyStyle" TargetType="ListBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ListBoxItem">
                <Grid Grid.IsSharedSizeScope="True">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Box" />
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Box" />
                        <ColumnDefinition Width="Auto" SharedSizeGroup="Box" />
                    </Grid.ColumnDefinitions>
                    <Border Grid.Column="0">
                        <TextBlock Text="{Binding PropertyA}" />
                    </Border>
                    <Border Grid.Column="1">
                        <TextBlock Text="{Binding PropertyB}" />
                    </Border>
                    <Border Grid.Column="2">
                        <TextBlock Text="{Binding PropertyC}" />
                    </Border>
                </Grid>

Instead of setting property Grid.IsSharedSizeScope="True" on Grid , set it on Listbox like this -不是在Grid上设置属性Grid.IsSharedSizeScope="True" ,而是像这样在 Listbox 上设置它 -

 <ListBox Grid.IsSharedSizeScope="True">

and remove it from the Grid .并将其从Grid删除。

This will make all the listboxItems to share the same size instead for an individual item.这将使all listboxItems share the same size而不是单个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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