C# WPF MVVM绑定动态控制在代码隐藏和传入视图中
我正在使用MVVM开发WPF应用程序。我有两页。我在第1页中有多个UserControls,在从第1页选择UserControls时,我想在第2页中显示所选的userControl。下面是我的代码 查看模型代码C# WPF MVVM绑定动态控制在代码隐藏和传入视图中,c#,wpf,mvvm,user-controls,datatemplate,C#,Wpf,Mvvm,User Controls,Datatemplate,我正在使用MVVM开发WPF应用程序。我有两页。我在第1页中有多个UserControls,在从第1页选择UserControls时,我想在第2页中显示所选的userControl。下面是我的代码 查看模型代码 public RelayCommand<string> OnClickSelectWidgetCommand => new RelayCommand<string>((setUserControlName) => { using
public RelayCommand<string> OnClickSelectWidgetCommand => new RelayCommand<string>((setUserControlName) =>
{
using (new CursorWait())
{
var MyContentControl = setUserControlName;
MessageBox.Show(MyContentControl);
//How to render UserControl to View?
}
}, true);
<StackPanel Background="Black" VerticalAlignment="Top">
<Border Name="UserControl1BorderLow" BorderBrush="White" BorderThickness="0" >
<ItemsControl ItemsSource="{Binding LowCollection}" Margin="4,0" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Left" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:UserControlColumn1XL HorizontalAlignment="Left" Margin="2" />
<!--what can I do here in above line to make it dynamically render the userControl in place of UserControlColumn1XL-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border></StackPanel>
public RelayCommand OnClickSelectWidgetCommand=>new RelayCommand((setUserControlName)=>
{
使用(新游标等待())
{
var MyContentControl=setUserControlName;
MessageBox.Show(MyContentControl);
//如何将UserControl呈现给视图?
}
},对);
在上面的代码中,我在setUserControlName变量中获得UserControl名称。现在如何将UserControl绑定到XAML页面?下面是我尝试过的代码
查看代码
public RelayCommand<string> OnClickSelectWidgetCommand => new RelayCommand<string>((setUserControlName) =>
{
using (new CursorWait())
{
var MyContentControl = setUserControlName;
MessageBox.Show(MyContentControl);
//How to render UserControl to View?
}
}, true);
<StackPanel Background="Black" VerticalAlignment="Top">
<Border Name="UserControl1BorderLow" BorderBrush="White" BorderThickness="0" >
<ItemsControl ItemsSource="{Binding LowCollection}" Margin="4,0" >
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel HorizontalAlignment="Left" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<controls:UserControlColumn1XL HorizontalAlignment="Left" Margin="2" />
<!--what can I do here in above line to make it dynamically render the userControl in place of UserControlColumn1XL-->
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</Border></StackPanel>
上面的代码,在DataTemplate中,动态绑定UserControls需要更改什么?有两种方法可以解决这个问题,一种是根据数据类型(DataTemplates)设置模板,另一种是根据数据本身(DataTriggers)设置模板 在第一种情况下,LowCollection应该是一个对象数组,或者是视图模型都从中派生出来的某个基类(ViewModel1、ViewModel2等)。在这种情况下,您可以完全删除itemtemplate,只需添加DataTemplates来指定ItemsControl中每个项的表示方式:
<ItemsControl.Resources>
<DataTemplate DataType="{x:Type local:ViewModel1}">
<UserControl1 />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ViewModel2}">
<UserControl2 />
</DataTemplate>
... etc...
... 等
在第二种情况下,需要根据视图模型中某些属性的值设置模板。在这种情况下,您确实需要设置ItemTemplate,并为其提供一种使用数据触发器设置适当DataTemplate的样式:
<ItemsControl.ItemTemplate>
<DataTemplate>
<ContentPresenter Content="{Binding}">
<ContentPresenter.Style>
<Style TargetType="{x:Type ContentPresenter}">
<Style.Triggers>
<DataTrigger Binding="{Binding YourProperty}" Value="YourValue1">
<Setter Property="ContentTemplate" Value="{StaticResource YourDataTemplate1}" />
</DataTrigger>
<DataTrigger Binding="{Binding YourProperty}" Value="YourValue2">
<Setter Property="ContentTemplate" Value="{StaticResource YourDataTemplate2}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
</DataTemplate>
</ItemsControl.ItemTemplate>
此处需要注意的相关部分是,视图模型中有一个名为
YourProperty
的属性,它可以有两个值,即YourValue1
或YourValue2
;然后,根据YourProperty
的值,上面的样式选择YourDataTemplate1
或YourDataTemplate2
,它不会触发我从viewModel代码绑定的另一个用户控件。你能帮我做这件事吗。如果你愿意,我可以分享我的代码。