我有一个定义为
的集合 public class BraiderList : ObservableCollection <Braider>
将对象定义为
public class Braider : INotifyPropertyChanged
用于显示数据的XAML代码如下所示。就目前而言,一切正常,但我想改变我的收藏中的项目的显示方式。我希望我的收藏中的每个项目都是包装面板中的单独项目,而不是它们都是一个控件的一部分。有没有办法在XAML中执行此操作,还是需要用C#编写代码?
<Border Grid.Row="1" BorderBrush="Black" BorderThickness="5" CornerRadius="8" Margin="2,2" ClipToBounds="True" >
<WrapPanel Name="WPanel1">
<Border Margin="5" Padding="5">
<ItemsControl Name="MyList">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Name="SPanel1">
<Image Source="{StaticResource BraiderImage}" Height ="75" Width="150" Visibility ="{Binding ShowIcon}"/>
<Label Content= "{Binding Name}" Visibility ="{Binding ShowName}" HorizontalAlignment="Center"/>
<Label Content= "{Binding ProductionCounter}" ContentStringFormat="Production Counter: {0:0.0}" Visibility ="{Binding ShowProductionCounter}" />
<Label Content= "{Binding LeadFront}" ContentStringFormat="Lead, Front Deck: {0:0.0}" Visibility ="{Binding ShowLeadFront}"/>
<Label Content= "{Binding LeadBack}" ContentStringFormat="Lead, Back Deck: {0:0.0}" Visibility ="{Binding ShowLeadBack}"/>
<Label Content= "{Binding Address}" ContentStringFormat="IP Address: {0}" Visibility ="{Binding ShowIP}"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border>
</WrapPanel>
</Border>
答案 0 :(得分:1)
您的XAML似乎错了。
您要找的是将WrapPanel
设置为ItemsControl
的{{3}},以便ItemsControl
使用WrapPanel
布置它的项目而不是在WrapPanel
本身内部,如下所示:
<!-- No Need for a WrapPanel outside the ItemsControl, remove it -->
<Border>
<ItemsControl Name="MyList">
<!-- use a WrapPanel as the ItemsPanel for this ItemsControl -->
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<!-- Rest of your XAML here -->
</ItemsControl>
</Border>