使用项控件(或类似)的正确方法
本文关键字:方法 控件 | 更新日期: 2023-09-27 18:33:51
所以,我正在尝试制作一个表示UML建模程序中类的用户控件。
问题是,到目前为止,我所做的事情在我自己看来似乎是一种错误的方式。
我希望它只能使用单个 ItemsControl 来完成。是吗?
<Border BorderThickness="1" BorderBrush="Black">
<DockPanel>
<TextBox Text="ClassName" HorizontalAlignment="Center" DockPanel.Dock="Top"/>
<ItemsControl Name="attributeList" ItemsSource="{Binding Attributes}" Margin="5,0,5,0" DockPanel.Dock="Top">
</ItemsControl>
<ItemsControl Name="propertiesList" ItemsSource="{Binding Properties}" Margin="5,0,5,0" DockPanel.Dock="Top">
</ItemsControl>
<ItemsControl Name="methodsList" ItemsSource="{Binding Methods}" Margin="5,0,5,0" DockPanel.Dock="Top">
</ItemsControl>
</DockPanel>
</Border>
如果您在一个集合中显示所有这些属性和属性,您肯定会错过一些东西......您将如何显示它们周围的框?当然,您需要多个ItemsControl
才能显示框吗?
<Grid Width="100" TextBlock.TextAlignment="Center">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ItemsControl Grid.Row="0" BorderBrush="Black" BorderThickness="1"
TextElement.FontWeight="Bold">Customer</ItemsControl>
<ItemsControl Grid.Row="1" BorderBrush="Black" BorderThickness="1,0,1,1"
ItemsSource="{Binding Attributes}" />
<ItemsControl Grid.Row="2" BorderBrush="Black" BorderThickness="1,0,1,1"
ItemsSource="{Binding Properties}" />
<ItemsControl Grid.Row="3" BorderBrush="Black" BorderThickness="1,0,1,1"
ItemsSource="{Binding Methods}" />
</Grid>
然后,您可以使用一些额外的属性和Converter
来扩展它以隐藏没有项目的部分:
public bool HasMethods
{
return Methods.Count > 0;
}
当然,您必须在更新这些属性的相关集合时提醒INotifyPropertyChanged.PropertyChangedEventHandler
注意这些属性。然后你可以像这样使用它:
<ItemsControl Grid.Row="3" BorderBrush="Black" BorderThickness="1,0,1,1"
ItemsSource="{Binding Methods}" Visibility="{Binding HasMethods,
Converter={StaticResource BoolToVisibilityConverter}}" />