Reputation: 58
I have the following XAML code:
<ListView Name="UserConfigurationListView" ItemsSource="{Binding Groups}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Path=GROUP_NAME}" />
<CheckBox IsChecked="{Binding GroupIsChecked}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListView>
Groups is table in my Database, which contain GROUP_NAME
column but doesn't contain GroupIsChecked
. So far I managed to display TextBlock but I dontt have idea how can I set Checkbox to true/false. The problem is that GroupIsChecked
is not in my table and I want to check some conditions to set it to true/false. Any idea?
Upvotes: 0
Views: 112
Reputation: 121
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>
Upvotes: 1
Reputation: 275
The main idea in MVVM is that there is a "middle" class that links your model to your view.
In your situation, the model class is the Database table object, and your view is your UI.
You need to write a ViewModel class which represents a row in your table, and there you can write any properties you want. A collection of these ViewModels will be your "middle" table. Bind this collection to your ListView.
The GroupIsChecked property should not be on your table, so you can implement its logic in your ViewModel class. Perhaps you can give the GROUP_NAME more appropriate name like GroupName, and so on for all your columns in the table.
Upvotes: 1