简体   繁体   English

WPF:不同用户控件的DataTemplate绑定

[英]WPF: DataTemplate binding for different usercontrol

I'm new on WPF and I'm tryng to figure out how to implement a binding with different type of user control.我是 WPF 的新手,我正在尝试弄清楚如何使用不同类型的用户控件实现绑定。 After the user clicks a button, a usercontrol (a simple shape like rectangle or ellipse) is added to the window.用户单击按钮后,将向 window 添加用户控件(矩形或椭圆等简单形状)。

I'm tryng to use an MVVM approach so the xaml appears as follow:我正在尝试使用 MVVM 方法,因此 xaml 如下所示:

    ...
    Title="{Binding Path=Titolo}"
    Height="450" Width="800"
    d:DataContext="{d:DesignInstance vm:MainWindowViewModel}">


<Canvas>
    <Button Content="Add Shape" Command="{Binding TestCommand}"/>
    <ItemsControl ItemsSource="{Binding RectCollection}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <uc:MyCustomRectangle/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>

        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Canvas.Left" Value="{Binding xLT}" />
                <Setter Property="Canvas.Top" Value="{Binding yLT}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
</Canvas>

All works fine but I want to add different type of UserControl (not only MyCustomRectangle) using the same button (for example, randomly adding rectangle or ellipse).一切正常,但我想使用相同的按钮(例如,随机添加矩形或椭圆)添加不同类型的 UserControl(不仅是 MyCustomRectangle)。 A possible solution could be duplicate the section of ItemsControl and select a different collection of binding:一个可能的解决方案可能是复制 ItemsControl 和 select 的部分不同的绑定集合:

<Canvas>
    <Button Content="Add Shape" Command="{Binding TestCommand}"/>
    <ItemsControl ItemsSource="{Binding RectCollection}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <uc:MyCustomRectangle/> <!-- bind to my usercontrol -->
            </DataTemplate>
        </ItemsControl.ItemTemplate>

        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Canvas.Left" Value="{Binding xLT}" />
                <Setter Property="Canvas.Top" Value="{Binding yLT}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
 
    <ItemsControl ItemsSource="{Binding EllipseCollection}">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <uc:MyCustomEllipse/>  <!-- bind to my usercontrol -->
            </DataTemplate>
        </ItemsControl.ItemTemplate>

        <ItemsControl.ItemContainerStyle>
            <Style>
                <Setter Property="Canvas.Left" Value="{Binding xLT}" />
                <Setter Property="Canvas.Top" Value="{Binding yLT}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
    </ItemsControl>
</Canvas>

I don't think this is the correct solution, especially because I would to add many different types of shapes (and also text and images).我认为这不是正确的解决方案,尤其是因为我要添加许多不同类型的形状(以及文本和图像)。 So, if exist, what is the correct way to bind a data template to different type of usercontrol?那么,如果存在,将数据模板绑定到不同类型的用户控件的正确方法是什么? Is MVVM the correct approach to solve this problem? MVVM 是解决此问题的正确方法吗?

What you can do is to bind to a global list with all Shapes.您可以做的是绑定到具有所有形状的全局列表。

And then you can define different DataTemplate s for different Types.然后你可以为不同的类型定义不同的DataTemplate

Like this:像这样:

<ItemsControl.Resources>
    <DataTemplate DataType="{x:Type MyType1}">
        ...
    </DataTemplate>
    <DataTemplate DataType="{x:Type MyType2}">
        ....
    </DataTemplate>
</ItemsControl.Resources>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM