選択したアイテムのビュー モデルで指定されたプロパティを使用する代わりに、グリッドを表示する xaml で次のようなものを使用できます。
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication3"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactions"
xmlns:ie="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="WpfApplication3.MainWindow"
x:Name="MyMainWindow">
<Grid>
<DataGrid x:Name="myGrid" ItemsSource="{Binding myItems}">
<ie:Interaction.Triggers>
<ie:EventTrigger EventName="SelectionChanged">
<ie:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}" CommandParameter="{Binding ElementName=myGrid, Path=SelectedItem}"/>
</ie:EventTrigger>
</ie:Interaction.Triggers>
</DataGrid>
</Grid>
次に、View Model で:
public class MainWindowViewModel
{
public MainWindowViewModel()
{
myItems = new ObservableCollection<Person>
{
new Person("John", 23),
new Person("Kobi", 25),
new Person("Lizard", 43)
};
SelectedItemChangedCommand = new DelegateCommand<object>((selectedItem) =>
{
var selected = selectedItem as Person;
// Do whatever you want to display the properties of your selected item
// and let you user change them
});
}
public ObservableCollection<Person> myItems { get; set; }
public DelegateCommand<object> SelectedItemChangedCommand { get; set; }
}
public class Person
{
public Person(string name, int age)
{
Name = name;
Age = age;
}
public string Name { get; set; }
public int Age { get; set; }
}