Reputation: 1452
I have the problem when I using CheckBox
in my GridView
using WPF and MVVM.
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
My ViewModel
public bool IsSelected
{
get
{
return _isSelected;
}
set
{
if (_isSelected == value)
{
return;
}
_isSelected = value;
OnPropertyChanged("IsSelected");
}
}
How I can select the data in a selected row, i.e. the CheckBox
value of that row is true?
Upvotes: 4
Views: 4229
Reputation: 23833
You will need to set up a IEnumerable<bool>
/IEnumerable<SomeClass>
that holds the IsChecked
information for each of your CheckBox
es. Something like
public class CheckedItem
{
public CheckedItem() { }
public CheckedItem(string text, bool isChecked) : this()
{
this._text = text;
this._isChecked = isChecked;
}
private string _text;
public String Text;
{
get { return _text; }
set
{
if (_text == value)
return;
_text = value;
OnPropertyChanged("IsSelected");
}
}
private bool _isSelected;
public bool IsSelected;
{
get { return _isSelected; }
set
{
if (_isSelected == value)
return;
_isSelected = value;
OnPropertyChanged("IsSelected");
}
}
}
private ObservableCollection<CheckedItem> checkItemCollection =
new ObservableCollection<CheckedItem>();
public ObservableCollection<CheckedItem> CheckItemCollection
{
get { return checkItemCollection; }
set
{
if (checkItemCollection == value)
return;
checkItemCollection = value;
OnPropertyChanged("CheckItemCollection");
}
}
in XAML you can then bind the GridView
to this using
<GridView ItemsSource="{Binding Path=CheckedItemCollection,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
IsAsync=True}"
<GridView.Columns>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox Content="{Binding Text}"
IsChecked="{Binding IsSelected,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged,
IsAsync=True}"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridView.Columns>
</GridView>
This was done without an IDE so you may have to tweak this to get it to work.
I hope this helps.
Upvotes: 3