이렇게하는 데는 여러 가지 방법이 있습니다. 먼저이 작업을 수행하는 툴킷을 사용하고 있는지 확인해야합니다. 이 툴킷의 설정 방법과 사용 방법을 확인한 후.
노트, 그것은 귀하의 경우되지 않을 수도 있습니다,하지만 그것은 단지
(새로 만들고 어떻게 작동하는지 볼 주시기) 이해의 첫번째에서 INotifyPropertyChanged를 구현하는 ViewModelBase를 작성, 바람직하게는, 다음 MainViewModel을 만들 ViewModelBase 클래스
public abstract class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public virtual ICommand NavigateCommand => new RelayCommand(Navigate);
protected virtual void Navigate(object param)
{
}
}
을이 추상 클래스를 만들 그것은 ViewModelBase을 상속했고, CRE 그것의 ViewModels
를 할당하는 속성을 먹은 public class MainViewModel: ViewModelBase
{
private ViewModelBase _currentViewModel;
public ViewModelBase CurrentViewModel
{
get => _currentViewModel;
set
{
if (_currentViewModel == value)
return;
_currentViewModel = value;
OnPropertyChanged(nameof(CurrentViewModel));
}
}
public MainViewModel()
{
}
protected override void Navigate(object args)
{
var namespaceName = "YourNameSpace.";
var className = args.ToString();
var fullClassName = string.Concat(namespaceName, (string)className);
if (string.IsNullOrEmpty(fullClassName))
return;
var tipo = Type.GetType(fullClassName);
if (tipo == null)
return;
var myObj = Activator.CreateInstance(tipo) as ViewModelBase;
if (myObj != null)
CurrentViewModel = (ViewModelBase)myObj;
}
}
당신이 MainViewModel
는 ViewModelBase
(이동)의 재정의 된 메서드를 가지고 있음을 유의 , 그것은 간단한 매개 변수에서 다른 ViewModels를 여는 데 사용됩니다이 방법이다 지금
을 열고 당신이 내 예제에서, 당신의 MainWindow
콘텐츠에 메뉴와 ViewModels로 채워집니다 컨텐츠를 추가하고자하는 뷰 모델의 이름을 가진 문자열 <Window x:Class="YourNameSpace.MainWindow"
.......
Title="MainWindow"
DataContext="{StaticResource MainViewModel}" >
<Grid>
.....
<!--In the Menu, how to open a View -->
<MenuItem Header="Open ViewA" Command="{Binding NavigateCommand}" CommandParameter="ViewModelA" />
.......
<!-- Your Content Control -->
<ContentControl
HorizontalAlignment="Center"
VerticalAlignment="Top"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Top"
ClipToBounds="True"
Content="{Binding CurrentViewModel}" >
</ContentControl>
.....
</Window>
보기를 만듭니다. 예 :
<Button Content="Open View A"
Command="{Binding DataContext.NavigateCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Mode=OneWay}"
CommandParameter="ViewModelA" Margin="5" />
:의 말을하자 app.xaml 파일에 지금 <UserControl x:Class="YourNameSpace.ucViewA"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
>
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Content="this is the ViewA" Margin="5" />
<Button Grid.Column="2" Content="Open View B"
Command="{Binding DataContext.NavigateCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}, Mode=OneWay}"
CommandParameter="ViewModelB" Margin="5" />
</Grid>
<DataGrid Grid.Row="2" ColumnWidth="*" Margin="5"
IsReadOnly="True"
AutoGenerateColumns="False"
SelectionMode="Single"
HorizontalContentAlignment="Center"
ScrollViewer.CanContentScroll="True"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Header="Column 1" MinWidth="50" Width="Auto"/>
<DataGridTextColumn Header="Column 2" MinWidth="140" Width="Auto"/>
<DataGridTextColumn Header="Column 3" MinWidth="240" Width="*"/>
<DataGridTextColumn Header="Column 4" MinWidth="70" Width="Auto"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
</UserControl>
는 뷰 모델은 다른보기는 다음과 같은 사용에서의 UserControl
<Application x:Class="YourNameSpace.App"
...
xmlns:local="clr-namespace:YourNameSpace"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:MainViewModel x:Key="MainViewModel" />
<DataTemplate DataType="{x:Type local:ViewModelA}">
<local:ucViewA />
</DataTemplate>
<DataTemplate DataType="{x:Type local:ViewModelB}">
<local:ucViewB />
</DataTemplate>
</Application.Resources>
</Application>
뷰를 열 수있는 속
편집 : 깜빡했는데, ViewModel에 적어도 하나의 생성자가 있어야합니다 (비어 있음). 그래서 prob가 없습니다. lem이 발생합니다.
고마워요, 이것도 제가 조금씩 다르지만 같은 원리입니다. – Remco