【发布时间】:2019-02-22 11:38:15
【问题描述】:
我想在一个视图中访问不同 ViewModel 的数据。
但是 DataContext 是完全不同的(MainView 中的 MainViewModel)。是否可以为每个窗口控件设置各自的 ViewModel?
还是只在 MainViewModel 中创建和引用 ObservableCollection<Student> Students 更好?
目前我想将 ViewModel StudentViewModel 中的属性 Students 分配给此 ComboBox。
MainViewModel(将 ApplicationViewModel 设置为 CurrentViewModel)
public class MainViewModel : ViewModelBase
{
public MainViewModel()
{
CurrentViewModel = ApplicationViewModel;
ShowStudentViewCommand = new RelayCommand(ShowStudentView);
}
public ViewModelBase CurrentViewModel
{
get => _currentViewModel;
set
{
if (_currentViewModel == value) return;
_currentViewModel = value;
RaisePropertyChanged("CurrentViewModel");
}
}
private ViewModelBase _currentViewModel;
private static readonly ApplicationViewModel ApplicationViewModel = new ApplicationViewModel();
private static readonly StudentViewModel StudentViewModel = new StudentViewModel();
public ICommand ShowStudentViewCommand { get; }
public ICommand ShowApplicationViewCommand { get; }
private void ShowStudentView()
{
CurrentViewModel = StudentViewModel;
}
private void ShowApplicationView()
{
CurrentViewModel = ApplicationViewModel;
}
}
ApplicationViewModel 和 StudentViewModel(加载数据并创建 ObservableCollection)
public class ApplicationViewModel : ViewModelBase
{
public ApplicationViewModel()
{
}
}
public class StudentViewModel : ViewModelBase
{
private ObservableCollection<Student> _students;
public StudentViewModel()
{
DataStudentService dataService = new DataStudentService();
Students = new ObservableCollection<Student>(dataService.GetAllStudents());
}
public ObservableCollection<Student> Students
{
get => _students;
private set
{
if (_students == value) return;
_students = value;
RaisePropertyChanged("Students");
}
}
}
MainView.xaml(设置要显示的 CurrentViewModel)
<Window x:Class="Test.View.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
Title="Test" Height="750" Width="700"
DataContext="{Binding Main, Source={StaticResource Locator}}">
<Grid>
<ContentControl Grid.Row="1" Content="{Binding CurrentViewModel}" />
</Grid>
</Window>
ApplicationView.xaml(当前显示)
<UserControl x:Class="Test.View.ApplicationView"
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"
xmlns:viewModel="clr-namespace:Test.ViewModel"
mc:Ignorable="d">
<Grid >
<ComboBox
TextSearch.TextPath=""
ItemsSource="{Binding Path=Students}"
DisplayMemberPath="Name"
SelectedValuePath="Name"
SelectedValue="{Binding Path=Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding Path=SelectedStudent, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
SelectedIndex="{Binding Path=SelectedStudentIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
IsSynchronizedWithCurrentItem="True"
IsEditable="True"
Width="200"
Margin="0,0,20,0"
VerticalContentAlignment="Center" />
</Grid>
</UserControl>
【问题讨论】:
-
请注意,同时绑定 SelectedValue、SelectedItem 和 SelectedIndex 看起来很奇怪。您通常只会绑定其中一个。在所有绑定上设置
Mode=TwoWay和UpdateSourceTrigger=PropertyChanged也是多余的。虽然这些设置已经是Selected...属性的默认值,但它们对ItemsSource绑定完全没有影响。 -
@Clemens:感谢您的提示,我使用 SelectedStudentIndex 重置选择(“-1”)。我正在尝试减少其他参数
-
一个ViewModel可以有(子)ViewModels的属性,没问题。
-
仅使用 SelectedValue,重置应该通过将 Name 设置为不存在的值来工作,例如空值。或者没有 SelectedValue(Path),将 SelectedItem 设置为 null。
-
是的,请注意它是
Binding Path=Students而不是Binding Property=Students。路径意味着它可以是 a.b.c
标签: c# wpf mvvm viewmodel mvvm-light