Я пытаюсь переключить содержимое элемента управления контентом с помощью ICommands. Теперь установка этого свойства работает в конструкторе, но не в любой из команд.
У меня есть это в моем app.xaml
<Application.Resources>
<vm:ViewModelLocator x:Key="Locator"
d:IsDataSource="True" />
<DataTemplate DataType="{x:Type vm:HomeViewModel}">
<views:HomeView />
</DataTemplate>
<DataTemplate DataType="{x:Type vm:DeviceViewModel}">
<views:DeviceView />
</DataTemplate>
</Application.Resources>
Это фрагмент из ShellView.xaml (это представление, которое содержит элемент управления содержимым, который я хочу изменить):
<ContentControl Content="{Binding Path=CurrentViewModel}" />
Другой фрагмент, показывающий привязку кнопки:
<Button Content="Button"
Height="23"
Name="button2"
Width="75"
Command="{Binding Path=DeviceViewCommand}" />
Вот конструктор из ShellViewModel. Как я уже сказал, установка CurrentViewModel работает здесь. (Вы заметите, что я установил устройство, а затем домой в качестве теста.)
public ShellViewModel()
{
CurrentViewModel = ShellViewModel._deviceViewModel;
CurrentViewModel = ShellViewModel._homeViewModel;
HomeViewCommand = new RelayCommand(() => ExecuteHomeViewCommand());
DeviceViewCommand = new RelayCommand(() => ExecuteDeviceViewCommand());
LogOut = new RelayCommand(() => LogOutExecute(), () => true);
}
private void ExecuteDeviceViewCommand()
{
CurrentViewModel = ShellViewModel._deviceViewModel;
}
Я что-то здесь не так делаю?
Это свойство также изменено для текущей модели вида. Должен был добавить это раньше.
public ViewModelBase CurrentViewModel
{
get
{
return _currentViewModel;
}
set
{
if (_currentViewModel == value)
return;
_currentViewModel = value;
RaisePropertyChanged("CurrentViewModel");
}
}