如何在MVVM中创建母版页?

时间:2015-08-23 07:27:14

标签: c# wpf mvvm

我想在mvvm中创建母版页。我创建了一个viewbox,其名称是用于显示我的usercontrol的容器,我有两个类RelayCommandViewModel

这是我的代码:

public class ViewModel
{
    MainWindow objMainWindow = new MainWindow();

    UserControls.History objHistory = new UserControls.History();
    UserControls.NewItem objNewItem = new UserControls.NewItem();
    UserControls.SideEffect objSideEffect = new UserControls.SideEffect();

    public ViewModel()
    {         
        OpenCommand = new RelayCommand(Open);

    }


    private ICommand openCommand;
    public ICommand OpenCommand
    {
        get { return openCommand; }
        set { openCommand = value; }
    }

    public void Open(object sender)
    {

        if (sender.ToString() == "btnHistory")
        {
            objMainWindow.Container.Child = objHistory;

        }

        if (sender.ToString() == "btnNewItem")
        {

        }

        if (sender.ToString() == "btnSideEffect")
        {

        }

    }

}

这是我的RelayCommand

public class RelayCommand:ICommand
{

   public RelayCommand(Action<object> _action)
   {
       actionCommand = _action;
   }

    private Action<object> actionCommand;




    public bool CanExecute(object parameter)
    {
        return true;
    }

    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    } 


    public void Execute(object parameter)
    {
        if (parameter !=null)
        {
            actionCommand(parameter);
        }

        else
        {
            actionCommand("Null");
        }
    }

}

但是当我运行解决方案时,我想要向我的孩子展示容器时遇到NullRefrenceException。 我不知道如何做这项工作。

1 个答案:

答案 0 :(得分:0)

您的MainWindow会在程序启动时实例化。因此,您不应该在ViewModel中重新实例化它(即此行:MainWindow objMainWindow = new MainWindow();)。您应该使用DataBinding代替。

以下示例代码为您提供了一个想法:

首先在FrameworkElement中定义ViewModel类型的属性,并将其值设置为UserControl方法中所需的Open

<强>视图模型:

public class ViewModel : INotifyPropertyChanged
{
    FrameworkElement _myUc;
    public FrameworkElement MyUserControl
    {
        get
        {
            return _myUc;
        }
        set
        {
            _myUc= value;
            OnPropertyChanged("MyUserControl");
        }
    }

    public ViewModel()
    {
        OpenCommand = new RelayCommand(Open);
    }

    public void Open(object sender)
    {
        if (sender.ToString() == "btnHistory")
        {
           MyUserControl = objHistory;
        }
    }

    // rest of your view model ...
}

然后将ViewModel实例化为DataContextMainWindow的{​​{1}}。

<强>主窗口:

Constructor

最后使用public ViewModel MyViewModel { get; set; } public MainWindow() { InitializeComponent(); MyViewModel = new ViewModel(); DataContext = MyViewModel; } (而不是ContentControl)[查看我的注释]并将其ViewBox绑定到{{1}您Content的属性。

<强> XAML:

MyUserControl

这种方式每次ViewModel更改时,<Grid > <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ContentControl Grid.Row="0" Content="{Binding MyUserControl}" x:Name="Container"/> <Button Grid.Row="1" Name="btnHistory" Content="ShowHistory" Command="{Binding OpenCommand}" /> </Grid> 都会显示您想要的MyUserControl

注意 ContentControl的{​​{1}}属性不是UserControl,因此无法绑定。