Student.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
namespace WpfDependencyApp.Model
{
public class Student:DependencyObject
{
public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student));
public string Name
{
set { SetValue(NameProperty, value); }
get { return (string)GetValue(NameProperty); }
}
public static readonly DependencyProperty AgeProperty = DependencyProperty.Register("Age", typeof(int), typeof(Student));
public int Age
{
set { SetValue(AgeProperty, value); }
get { return (int)GetValue(AgeProperty); }
}
public override string ToString()
{
return this.Name;
}
}
}
ViewModel.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WpfDependencyApp.Model;
namespace WpfDependencyApp.ViewModel
{
public class MainViewModel
{
private List<Student> students = new List<Student>();
public MainViewModel()
{
Students = new List<Student>()
{
new Student{Name="chen1",Age=100},
new Student{Name="chen2",Age=101},
};
ButtonCommand = new RelayCommand(() =>
{
testCommand();
});
Selected = Students[0];
}
public List<Student> Students { get => students; set => students = value; }
private Student selected = new Student();
public Student Selected { get => selected; set => selected = value; }
public ICommand ButtonCommand { get; private set; }
public void testCommand()
{
this.Selected.Age = 102;
}
}
/// <summary>
/// the command without input parameter
/// </summary>
public class RelayCommand : ICommand
{
public event EventHandler CanExecuteChanged = (sender, e) => { };
private Action mAction;
public RelayCommand(Action action)
{
this.mAction = action;
}
public bool CanExecute(object parameter)
{
// Always allow the action can execute.
return true;
}
public void Execute(object parameter)
{
// Take action
mAction.Invoke();
}
}
}
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfDependencyApp.ViewModel;
namespace WpfDependencyApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainViewModel();
}
}
}
xaml.cs
<Window x:Class="WpfDependencyApp.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:WpfDependencyApp"
xmlns:test="clr-namespace:WpfDependencyApp.ViewModel"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<ComboBox x:Name="test" ItemsSource="{Binding Students}" SelectedItem="{Binding Selected}" DisplayMemberPath="Age"/>
<Button Height="40" Command="{Binding ButtonCommand}"></Button>
</StackPanel>
</Window>