![](/img/trans.png)
[英]how to measure grid width/height in wpf dynamically as the window size changes
[英]How to set a specific height/width for all elements in a WPF grid?
我有一個自定義 ListBox,其中每個項目都有一個 Grid 定義在我顯示框的位置。 我可以使用 SharedSizeGroup 屬性將寬度設置為相同的長度。 但是,如何為父 ListBox 元素中的所有這些項目共享此屬性?
我也嘗試在 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>
不是在Grid
上設置屬性Grid.IsSharedSizeScope="True"
,而是像這樣在 Listbox 上設置它 -
<ListBox Grid.IsSharedSizeScope="True">
並將其從Grid
刪除。
這將使all
listboxItems
share the same size
而不是單個項目。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.