MVVMパターンについて読んで始めてください。
モデルが必要です。 INotifyPropertyChangedインターフェイスを実装する必要があります。また、各プロパティー設定者はOnPropertyChanged()メソッドを呼び出す必要があります。実装私はあなたに任せます。
public class Model
{
public string Name { get; set; }
public bool IsChecked { get; set; }
}
ビューモデルが必要です。
public class ViewModel
{
public ObservableCollection<Model> MyList { get; set; }
public ViewModel()
{
MyList = new ObservableCollection<Model>();
MyList.Add(new Model() { Name = "John", IsChecked = true });
MyList.Add(new Model() { Name = "Bety", IsChecked = false });
MyList.Add(new Model() { Name = "Samuel", IsChecked = true });
}
}
ビューでの右バインディング。
<Window x:Class="WpfApp4.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:WpfApp4"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:ViewModel></local:ViewModel>
</Window.DataContext>
<Grid>
<DataGrid ItemsSource="{Binding MyList}" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Description" Binding="{Binding Name}"/>
<DataGridCheckBoxColumn Header="Select" Binding="{Binding IsChecked, UpdateSourceTrigger=PropertyChanged}"/>
</DataGrid.Columns>
</DataGrid>
</Grid>
そして、あなたは、ビュー・モデルにマイリストを反復し、チェック/チェックなしのアイテムでやりたいです。
助けを得る最良の方法は、いくつかのコードを提供することです。あなたの現在の作業進行中でないように。選択したアイテム 'dataGrid.selectedItems'を簡単に取得できます。しかし、さらなる情報は、私たちがあなたを助けるのに役立ちます。例えば:選択をどこで手に入れたいですか?あなたのアーキテクチャはどのようなものですか? CodeBehindまたはXAMLで選択してもらいたいですか?... – Jodn