OrMiz is correct about implementing a view model for your view.
I'm not an MVVM expert, but here is one example of how you might do that.
Feedback welcome.
The class that represents your group table...
public class GroupTable
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
}
A view model glass that could represent one row in your list view...
public class GroupViewModel : INotifyPropertyChanged
{
public GroupViewModel(GroupTable group)
{
this.GroupName = group.Name;
this.GroupIsChecked = GroupCheckedLogic();
}
private string _GroupName;
public string GroupName
{
get { return _GroupName; }
set
{
_GroupName = value;
OnPropertyChanged();
}
}
private bool _GroupIsChecked;
public bool GroupIsChecked
{
get { return _GroupIsChecked; }
set
{
_GroupIsChecked = value;
OnPropertyChanged();
}
}
private bool GroupCheckedLogic()
{
// Your logic goes here
return true;
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Then a main view model for the view...
public class GroupListViewModel : INotifyPropertyChanged
{
public GroupListViewModel()
{
// Get your actual data
var groupsData = new List<GroupViewModel>
{
new GroupViewModel(new GroupTable {Id = 1, Name = "Group 1"}),
new GroupViewModel(new GroupTable {Id = 2, Name = "Group 2"})
};
this.Groups = new ObservableCollection<GroupViewModel>(groupsData);
}
private ObservableCollection<GroupViewModel> _Groups;
public ObservableCollection<GroupViewModel> Groups
{
get { return _Groups; }
set
{
_Groups = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
Then in your XAML...
<ListView Name="UserConfigurationListView" ItemsSource="{Binding Groups}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding GroupName}" />
<CheckBox IsChecked="{Binding GroupIsChecked}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListView>
Groupsis a table or C# collection ofGroupobjects? I hope you are doing it MVVM way. In that case, you can have a propertyIsCheckedproperty inGroupclass.