Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以MVVM方式添加控件,但不要';不能直接声明控件类型_C#_Wpf - Fatal编程技术网

C# 以MVVM方式添加控件,但不要';不能直接声明控件类型

C# 以MVVM方式添加控件,但不要';不能直接声明控件类型,c#,wpf,C#,Wpf,黑点问题 是否可以以MVVM方式添加/生成控件,而不在XAML代码中声明控件类型?很难用一个词来表达,所以让我们举个例子: SQL Server中有控件表,其列如下: 1.控制名 2.控件类型 3.控制绑定(?) 现在,在我的ViewModel中,我声明了一个observateCollection,它将是我想要拥有的那些控件的集合,并从controls表中填充它。当然是通过绑定到itemsource 据我所知,动态添加控件很简单,但仅当我们声明类型为straight时,例如: <Sta

黑点问题

是否可以以MVVM方式添加/生成控件,而不在XAML代码中声明控件类型?很难用一个词来表达,所以让我们举个例子:

SQL Server中有
控件
表,其列如下:

1.控制名
2.控件类型
3.控制绑定(?)

现在,在我的
ViewModel
中,我声明了一个
observateCollection
,它将是我想要拥有的那些控件的集合,并从
controls
表中填充它。当然是通过绑定到
itemsource

据我所知,动态添加控件很简单,但仅当我们声明类型为straight时,例如:

<StackPanel>
    <ItemsControl ItemsSource="{Binding SomeCollection}">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <TextBox Text="{Binding Path=.}"/>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>
</StackPanel>


除了从数据库中获取控件类型之外,还有其他方法可以做同样的事情吗?

我必须创建
DataTemplates
并实现
DataTemplateSelector

<DataTemplate x:Key="TextBoxDataTemplate">
        <Grid VerticalAlignment="Top" HorizontalAlignment="Left">
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Width="200" FontSize="15" FontWeight="Bold" Text="{Binding ControlName}" TextWrapping="Wrap"/>
            <TextBox VerticalAlignment="Top" HorizontalAlignment="Left" Margin="205,0,0,0" Width="616" Height="35" FontSize="12" Text="{Binding ControlValue,UpdateSourceTrigger=PropertyChanged}"/>
        </Grid>
    </DataTemplate>
    <DataTemplate x:Key="ComboBoxDataTemplate">
        <Grid VerticalAlignment="Top" HorizontalAlignment="Left">
            <TextBlock VerticalAlignment="Center" HorizontalAlignment="Left" Width="200" FontSize="15" FontWeight="Bold" Text="{Binding ControlName}" TextWrapping="Wrap"/>
            <ComboBox VerticalAlignment="Top" HorizontalAlignment="Left" Margin="205,0,0,0" Width="616" Height="35" FontSize="15"/>
        </Grid>
    </DataTemplate>
然后我只需在我的
ItemsControl
中绑定
ItemsSource
ItemTemplateSelector
,如下所示:

<ItemsControl ItemsSource="{Binding ControlsCollection}" ItemTemplateSelector="{StaticResource ControlTemplateSelector}"/>


ControlsCollection
是从数据库填充的。

是否必须生成VM?如果不同集合项类型有固定的数据模板集,请将数据模板声明为资源并设置其数据类型属性。为了更具动态性,编写一个DataTemplateSelector并将其分配给ItemsControl的ItemTemplateSelector属性。@MickyD你是什么意思?生成除主ViewModel之外的VM?我的意思是,如果您的目的是定义DB中的所有控件,从而消除对V的更改,那么如何进行绑定?我可以想出一种方法,但前提是我对整个V和VMV进行编码。当然,您不需要将数据模板添加到ItemsSource集合中。先看一看。
<ItemsControl ItemsSource="{Binding ControlsCollection}" ItemTemplateSelector="{StaticResource ControlTemplateSelector}"/>