[英]Change the User Control of a Window dynamically in WPF (MVVM)
我是WPF的新手,我只是在使用帶有綁定和命令的 MVVM做一個簡單的菜單但是我覺得我做錯了。 我只想更改導入我定義的新UserControl
所有Window Content,每次按下菜單按鈕。 這意味着我想要消失菜單並顯示一個新內容(我之前提到過的UserControls)。
好吧,我有主窗口 (ControlPanel):
<Window x:Class="OfficeMenu.Views.Menu.ControlPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Control Panel" Height="800" Width="800">
<Grid>
<ContentControl Content="{Binding contentWindow}"/>
</Grid>
</Window>
這個UserControls之一,在我運行項目時為主窗口提供了一個按鈕菜單:
<UserControl x:Class="OfficeMenu.Views.ButtonsMenu"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<!-- One style for each *type* of control on the window -->
<Style TargetType="Button">
<Setter Property="Margin" Value="5"/>
</Style>
</UserControl.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*" />
<RowDefinition Height="1*" />
</Grid.RowDefinitions>
<Button Command="{Binding OpenUsersCommand}">BUTTON 1</Button>
<Button Grid.Column="1">BUTTON 2</Button>
<Button Grid.Column="1" Grid.Row="2" >BUTTON 3</Button>
<Button Grid.Row="1">BUTTON 4</Button>
</Grid>
</UserControl>
這是我使用的ViewModel:
class MenuViewModel : ViewModelBase
{
RandomModel _model; <!-- It does nothing important -->
private UserControl _content;
public ICommand OpenUsersCommand { get; private set; }
public UserControl Content
{
get { return _content; }
set {
_content = value;
NotifyPropertyChanged("contentWindow");
}
}
public MenuViewModel(RandomModel model )
{
this._model= model;
OpenUsersCommand = new RelayCommand(OpenUsers,null);
}
private void OpenUsers()
{
Content = new UsersPanel(); //This is the UserControl we need to load dinamically
}
}
在App.xaml.cs中我們加載主窗口:
public partial class App : Application
{
private void OnStartup(object sender, StartupEventArgs e)
{
ControlPanel view = new ControlPanel();
view.ShowDialog();
}
}
現在,controlPanel.xaml.cs:
public ControlPanel()
{
InitializeComponent();
ModelRandom model = new ModelRandom(); <!-- It does nothing yet -->
MenuViewModel viewmodel = new MenuViewModel(model);
Content = new BottonsMenu(); <!-- We load a default UserControl when we run the program -->
this.DataContext = viewmodel;
}
}
非常感謝你。
我認為問題是您正在嘗試更改ContentControl的Content屬性,而不是ViewModel的屬性。 您已將ViewModel連接到主機ControlPanel,但您還需要為將在其中托管的用戶控件的單獨視圖模型。 為了清晰起見,我為用戶控件視圖模型添加了類並更改了主機的視圖模型屬性名稱。 按如下方式更正代碼。
//host view model
class MainModel : ViewModelBase
{
private UserControl _content;
public MainModel() { }
internal void SetNewContent(UserControl _content)
{
ContentWindow = _content;
}
public UserControl ContentWindow
{
get { return _content; }
set
{
_content = value;
OnPropertyChanged("ContentWindow");
}
}
}
//user contol's view model
class MenuViewModel : ViewModelBase
{
MainModel _mainModel;
RandomModel _model; // <!-- It does nothing important -->
public ICommand OpenUsersCommand { get; private set; }
public MenuViewModel(MainModel mainModel, RandomModel model )
{
this._mainModel = mainModel;
this._model = model;
OpenUsersCommand = new RelayCommand(OpenUsers, CanOpenUsers);
}
private void OpenUsers(object _param)
{
UsersPanelViewModel upmodel = new UsersPanelViewModel(_mainModel, _model);
UsersPanel up = new UsersPanel();
up.DataContext = upmodel;
_mainModel.SetNewContent(up);
}
private bool CanOpenUsers(object _param)
{
return true;
}
}
//main window function
public ControlPanel()
{
InitializeComponent();
//create main view model for host
MainModel mainModel = new MainModel();
RandomModel model = new RandomModel(); //<!-- It does nothing yet -->
//create view model for user controls
MenuViewModel viewmodel = new MenuViewModel(mainModel, model);
ButtonsMenu bm = new ButtonsMenu(); // <!-- We load a default UserControl when we run the program -->
bm.DataContext = viewmodel;
//set host's property in our user control
mainModel.ContentWindow = bm;
this.DataContext = mainModel;
}
主窗口XAML
<Window x:Class="WpfApplication1.ControlPanel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ContentControl Content="{Binding ContentWindow}"/>
</Grid>
</Window>
希望這是可以理解的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.