C# WPF同步列表框的大小';s项目
我正在使用以下xaml:C# WPF同步列表框的大小';s项目,c#,wpf,xaml,data-binding,listbox,C#,Wpf,Xaml,Data Binding,Listbox,我正在使用以下xaml: <ListBox Name="Lst" ItemsSource="{Binding Items}" HorizontalContentAlignment="Stretch" > <ListBox.ItemsPanel> <ItemsPanelTemplate> <WrapPanel IsItemsHost="True" Orientation="Horiz
<ListBox Name="Lst" ItemsSource="{Binding Items}" HorizontalContentAlignment="Stretch" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="500" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Black" >
<Grid >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Id" />
<Label Grid.Row="0" Grid.Column="1" Content="{Binding Id}" />
<Label Grid.Row="1" Grid.Column="0" Content="Name" />
<Label Grid.Row="1" Grid.Column="1" Content="{Binding Name}" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
现在我得到了这个结果:
正如您所看到的,列表框中的项目大小不同,因为DataContext
的属性名称不同。但我想得到类似尺寸的物品,如下所示:
你能帮我回答这个问题吗?提前感谢
<ListBox>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Width" Value="200" />
<Setter Property="Height" Value="200" />
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
您应该使用网格列SharedSizeGroup
属性()
试试这个:
<ListBox Name="Lst" ItemsSource="{Binding Items}" HorizontalContentAlignment="Stretch"
Grid.IsSharedSizeScope="True">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="500" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Black" >
<Grid >
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition SharedSizeGroup="A" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Id" />
<Label Grid.Row="0" Grid.Column="1" Content="{Binding Id}" />
<Label Grid.Row="1" Grid.Column="0" Content="Name" />
<Label Grid.Row="1" Grid.Column="1" Content="{Binding Name}" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
在列表框上设置网格。IsSharedSizeScope=“True”,并为列指定
共享SizeGroup
,以便所有列共享相同的大小(项目之间的最大值)
这将得到您想要的结果:
<ListBox Name="Lst" ItemsSource="{Binding Items}" Grid.IsSharedSizeScope="True"
HorizontalContentAlignment="Stretch" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="500" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Black" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" />
<ColumnDefinition SharedSizeGroup="B"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Id" />
<Label Grid.Row="0" Grid.Column="1" Content="{Binding Description}" />
<Label Grid.Row="1" Grid.Column="0" Content="Name" />
<Label Grid.Row="1" Grid.Column="1" Content="{Binding Name}" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您希望您的盒子看起来是什么样的?你的名字不是约翰。。。我们不知道您想要什么,请举例说明。我希望我的盒子大小与其内容相同。那么大小是多少?“John”的尺寸或最长的尺寸,还是需要换行符?
<ListBox Name="Lst" ItemsSource="{Binding Items}" Grid.IsSharedSizeScope="True"
HorizontalContentAlignment="Stretch" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="500" />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<Border BorderThickness="1" BorderBrush="Black" >
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition SharedSizeGroup="A" />
<ColumnDefinition SharedSizeGroup="B"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Id" />
<Label Grid.Row="0" Grid.Column="1" Content="{Binding Description}" />
<Label Grid.Row="1" Grid.Column="0" Content="Name" />
<Label Grid.Row="1" Grid.Column="1" Content="{Binding Name}" />
</Grid>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>