用户控件的WPF MVVM动态绑定并根据所选设计显示记录

时间:2018-08-03 05:53:37

标签: c# wpf xaml mvvm data-binding

我的项目需要帮助。无法获得所需的适当解决方案。我在这里向您解释我的情况。

我正在使用MVVM模式在WPF项目上工作,这里有主Windows屏幕,在主Windows屏幕上加载时,它应该为空白,并且只有一个标签。单击该标签后,我必须打开一个新表单,即“小部件屏幕”和“小部件屏幕具有多个带有图像的按钮(数量为12)”。

现在,在单击任何按钮时,我想要特定的按钮名称或参数,因此,该名称或参数必须从视图中找到用户控件,并在主Windows屏幕上呈现20个记录的用户控件。如果再次单击“主Windows”标签并从小部件屏幕中选择另一个按钮,则它将加载另一个选定的“用户控件”,并将其与“主Windows”屏幕绑定。

因此,简而言之,根据小部件屏幕中选定的设计,所有记录都应使用用户控件显示在主Windows中。 这是我的小部件屏幕,从中我可以选择在主窗口屏幕中显示的集合设计。 enter image description here

这是我的主Windows屏幕代码,当前它是将我直接提到的静态用户控件绑定到控件中,而这12个用户控件之一是UserControlColumn1XL,应该如何在整个场景中动态更改它?

<Window x:Class="TechMedic.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:uc ="clr-namespace:TechMedic.View.UserControls"
    xmlns:local="clr-namespace:TechMedic"
    xmlns:controls="clr-namespace:TechMedic.View.UserControls"
    xmlns:data="clr-namespace:TechMedic.Data"
    mc:Ignorable="d"
    WindowStartupLocation="CenterScreen" Background="Black"
    Title="Main Window}" WindowState="Maximized" MinWidth="750" >

<StackPanel>    
    <StackPanel Grid.Column="0" Background="Black" VerticalAlignment="Top" >
        <Border BorderThickness="1" Background="Black" BorderBrush="White">
            <Button Style="{StaticResource TransparentStyle}" Command="{Binding OnClickNormalCommand}" CommandParameter="Normal" Foreground="LightSkyBlue" HorizontalAlignment="Center" FontSize="12pt" >
                <TextBlock Text="{Binding NormalCollection.Count, StringFormat='Normal: {0}'}" Padding="0,5, 0, 5" Foreground="{Binding IsNormalColor}" />
            </Button>
            </Border>
            <Border Name="UserControl1BorderNormal" BorderBrush="White" BorderThickness="0" >
                <ItemsControl ItemsSource="{Binding NormalCollection}" Name="ListNormal" Margin="4,0" >
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <WrapPanel HorizontalAlignment="Left" />
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <controls:UserControlColumn1XL HorizontalAlignment="Left" Margin="2" />
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </Border>
        </StackPanel>
</StackPanel>

0 个答案:

没有答案