C# 在WPF Itemscontrol中,未正确显示结果,它将所有记录合并到一个位置
我在记录的基础上装饰我的ItemsControl,但是它相互重叠了所有记录,我已经创建了输出的屏幕截图,我正在粘贴相同的xaml部分C# 在WPF Itemscontrol中,未正确显示结果,它将所有记录合并到一个位置,c#,wpf,itemscontrol,C#,Wpf,Itemscontrol,我在记录的基础上装饰我的ItemsControl,但是它相互重叠了所有记录,我已经创建了输出的屏幕截图,我正在粘贴相同的xaml部分 <Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" Grid.Row="1" > <ItemsControl x:Name="productsList" ItemsSource="{Binding Produ
<Border CornerRadius="6" BorderBrush="Gray" Background="LightGray" BorderThickness="2" Grid.Row="1" >
<ItemsControl x:Name="productsList" ItemsSource="{Binding ProductsCollection}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal" Margin="10" Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Left">
<StackPanel VerticalAlignment="Top" HorizontalAlignment="Left" Orientation="Vertical">
<TextBlock Text="{Binding Title, Mode=TwoWay}" Style="{DynamicResource DataLabel}"/>
</StackPanel>
<StackPanel Margin="5" VerticalAlignment="Top" HorizontalAlignment="Left" Orientation="Vertical">
<Button x:Name="btnEdit" Click="btnEdit_Click" Tag="{Binding}" Style="{StaticResource EditButtonStyle}" Content="Edit" />
<Button x:Name="btnDelete" Click="btnDelete_Click" Tag="{Binding}" Style="{StaticResource DeleteButtonStyle}" Content="Delete" />
</StackPanel>
</StackPanel>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Style.Setters>
<Setter Property="Grid.Row" Value="{Binding MyRow}" />
<Setter Property="Grid.Column" Value="{Binding MyColumn}" />
</Style.Setters>
</Style>
</ItemsControl.ItemContainerStyle>
<ItemsControl.Template>
<ControlTemplate>
<ScrollViewer x:Name="ScrollViewer" Padding="{TemplateBinding Padding}">
<ItemsPresenter />
</ScrollViewer>
</ControlTemplate>
</ItemsControl.Template>
</ItemsControl>
</Border>
我已通过删除以下代码表单ItemsPanelTemplate解决了此问题。我用WrapPanel代替了网格
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
</Grid>
以上代码替换为以下代码
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
即使绑定了Grid.Row
/Grid.Column
,ItemsPanelTemplate中的网格也只有一行/列。所以Grid.Row/Grid.Column绑定没有任何区别。您需要多个RowDefinition/ColumnDefinition sYes ASh,我刚看到它就删除了它们。