程序功能:有两个ComboBox,第一个ComboBox的ComboBoxItem为Name、Age和Sex,第二个ComboBox的ComboBoxItem为一个类,类中包含了Name、Age和Sex三个字段;根据第一个ComboBox选中的内容,来确定第二个ComboBox要显示字段。
同时,当鼠标进入第二个ComboBox时,会显示ToolTip,即完成一个控件的多个操作。
- NotificationObject.cs
using System.ComponentModel;
namespace MVVMDemo3.ViewModel
{
class NotificationObject : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
}
- DelegateCommand.cs
using System;
using System.Windows.Input;
namespace MVVMDemo3.Commands
{
class DelegateCommand : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter)
{
if (CanExecuteFunc == null)
{
return true;
}
return CanExecuteFunc(parameter);
}
public void Execute(object parameter)
{
if (ExecuteAction == null)
{
return;
}
ExecuteAction(parameter);
}
public Action<object> ExecuteAction { set; get; }
public Func<object, bool> CanExecuteFunc { get; set; }
}
}
- Student.cs
namespace MVVMDemo3.Models
{
class Student
{
public string Name { get; set; }
public int Age { get; set; }
public string Sex { get; set; }
}
}
- MainWindowViewModel.cs
using System;
using MVVMDemo3.Commands;
using MVVMDemo3.Models;
using System.Collections.Generic;
namespace MVVMDemo3.ViewModel
{
class MainWindowViewModel : NotificationObject
{
private string currentStudent;
public string CurrentStudent
{
get { return currentStudent; }
set
{
currentStudent = value;
RaisePropertyChanged("CurrentStudent");
}
}
private int selectedIndex;
public int SelectedIndex
{
get { return selectedIndex; }
set
{
selectedIndex = value;
RaisePropertyChanged("SelectedIndex");
}
}
private List<Student> studentInformations;
public List<Student> StudentInformations
{
get { return studentInformations; }
set
{
studentInformations = value;
RaisePropertyChanged("StudentInformations");
}
}
private string showContent;
public string ShowContent
{
get { return showContent; }
set
{
showContent = value;
RaisePropertyChanged("ShowContent");
}
}
public DelegateCommand SelectedCommand { get; set; }
public void Selected(object parameter)
{
CurrentStudent = StudentInformations[SelectedIndex].Name + " " + StudentInformations[SelectedIndex].Age.ToString() + " " +
StudentInformations[SelectedIndex].Sex;
if (SelectedIndex == 0)
{
ShowContent = "Name";
}
else if (SelectedIndex == 1)
{
ShowContent = "Age";
}
else
{
ShowContent = "Sex";
}
}
public MainWindowViewModel()
{
StudentInformations = new List<Student>
{
new Student()
{
Name = "Tom",
Age = 20,
Sex = "Man",
},
new Student()
{
Name = "Jack",
Age = 21,
Sex = "Man",
},
new Student()
{
Name = "Lucy",
Age = 22,
Sex = "Woman",
},
};
SelectedIndex = 0;
ShowContent = "Name";
CurrentStudent = StudentInformations[0].Name + " " + StudentInformations[0].Age.ToString() + " " + StudentInformations[0].Sex;
SelectedCommand = new DelegateCommand()
{
ExecuteAction = new Action<object>(Selected),
};
}
}
}
- 主xaml文件
<Window x:Class="MVVMDemo3.MainWindow"
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"
xmlns:local="clr-namespace:MVVMDemo3.ViewModel"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="MainWindow" Height="150" Width="400">
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid Margin="5">
<StackPanel Margin="5">
<TextBlock Margin="5" Width="300" Height="30" VerticalAlignment="Center" Text="{Binding CurrentStudent}"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<ComboBox Height="30" VerticalContentAlignment="Center" Margin="5" SelectedIndex="{Binding SelectedIndex}">
<ComboBoxItem Content="Name"/>
<ComboBoxItem Content="Age"/>
<ComboBoxItem Content="Sex"/>
</ComboBox>
<ComboBox Width="250" Height="30" VerticalContentAlignment="Center" Margin="5"
ToolTip="{Binding ShowContent}"
ItemsSource="{Binding StudentInformations}"
DisplayMemberPath="{Binding ShowContent}"
SelectedIndex="{Binding SelectedIndex}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding SelectedCommand}"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseEnter">
<i:InvokeCommandAction Command="{Binding TooltipCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ComboBox>
</StackPanel>
</StackPanel>
</Grid>
</Window>
- 完整代码
https://download.youkuaiyun.com/download/u012366767/10620107