使用Itemscontrol的正确方法(或类似)

时间:2013-10-03 16:40:39

标签: c# wpf itemscontrol

所以,我正在尝试创建一个代表UML建模程序中的类的UserControl。

问题是,到目前为止我所做的似乎是在我自己的眼中做错的方式。

我希望只使用一个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>

1 个答案:

答案 0 :(得分:0)

如果你在一个集合中显示所有这些属性和属性,肯定会错过任何东西......你会如何显示它们周围的方框?当然你需要多个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}}" />