[英]WPF listbox alignment
I am pulling some data from DB, and I would like to have that data in some kind of columns aligned along the listbox, but I keep getting elements next to each other. 我正在从数据库中提取一些数据,我希望这些数据沿着列表框在某些列中对齐,但是我一直使元素彼此相邻。
What I have: 我有的:
el1 el1
longEl2 longEl2
What I want 我想要的是
el1 el1
longEl2 longEl2
Code: 码:
<ListBox Grid.Row="2" ItemsSource="{Binding lb}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<CheckBox Grid.Column="0" Margin="10" Content="{Binding Name}" IsChecked="{Binding IsActive}"/>
<TextBlock Margin="10" Grid.Column="1">test</TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
With ListBox or generally every ItemsControl to contain Grids internally for every row and make the grid columns equal in each row you can use the SharedSizeScope . 使用ListBox或通常每个ItemsControl在内部为每一行包含网格,并使每一行中的网格列相等,您可以使用SharedSizeScope 。 You define
Grid.IsSharedSizeScope="True"
on the control and name the grid columns with SharedSizeGroup="ColumnName"
in the data template. 您在控件上定义
Grid.IsSharedSizeScope="True"
,并在数据模板中使用SharedSizeGroup="ColumnName"
命名网格列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.