Add New Usercontrol On按钮单击WPF MVVM中的命令

时间:2015-08-05 12:16:53

标签: c# wpf mvvm

您好我正在尝试动态显示usercontrol但它无法正常工作...请帮助我改进代码。 在MainWindowViewModel的构造函数中,我尝试设置contentcontrol的初始属性。 提前谢谢

<Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:VM="clr-namespace:WpfApplication1.ViewModel"
            xmlns:View="clr-namespace:WpfApplication1.View"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <DataTemplate DataType="{x:Type VM:FirstControlViewModel}">
                <View:FirstControl></View:FirstControl>
            </DataTemplate>
            <DataTemplate DataType="{x:Type VM:SecondControlViewModel}">
                <View:SecondControl></View:SecondControl>
            </DataTemplate>

        </Window.Resources>
        <Grid>
            <ContentControl Content="{Binding LoadedControl}" />
        </Grid>
    </Window>

查看型号代码: -

    public class MainViewModel: INotifyPropertyChanged        
    {
       public MainViewModel()
       {
           LoadedControl = "FirstControlViewModel";// here i am setting property  
                                                   //But not working
       }

       private string _LoadedControl;

       public string LoadedControl
       {
           get { return _LoadedControl; }
           set { _LoadedControl = value;

           NotifyPropertyChanged("LoadedControl");

           }
       }

1 个答案:

答案 0 :(得分:4)

您需要将ErrorDocument 403 http://somedomen.com/index.php/errorpage order allow,deny deny from 1.2.3.4 allow from all 设置为ViewModel类型的实例,而不是字符串!

LoadedControl