I am trying to figure out how to modify the state on a checkbox that is embedded in a ListView/GridView. The problem is that the checkbox is dependent upon the approval status of some records in a separate DataGrid. (eg. I am using the checkbox as an approval status True = all approved, False = none, null = some approved).
Since the DataContext is an Entity it doesn’t have a bool value that I could use to handle it.
<ListView x:Name="EmployeeNameListBox" Height="330" ItemsSource="{Binding}" SelectedValuePath="ID" SelectionChanged="EmployeeNameListBoxSelectionChanged">
<ListView.View>
<GridView AllowsColumnReorder="False" >
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsThreeState="True" Checked="EmployeeCheckBoxChecked" Unchecked="EmployeeCheckBoxChecked" x:Name="CheckBoxHero"/>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1} - {2}">
<Binding Path="LastName" />
<Binding Path="FirstName" />
<Binding Path="EmployeeNumber" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
Here is where I’m trying to update the approval status:
foreach (var employee in this.employees)
{
var records = from a in this.dailyActivities where a.Employee == employee select a;
var approvedRecords = from r in records where r.IsApproved == true select r;
if (approvedRecords.Count() == 0)
{
// None Approved checkbox state = false
}
else if (approvedRecords.Count() == records.Count())
{
// All Approved checkbox state = true
}
else
{
// Some Approved Set Checkbox state to null
}
}
Create a
Nullable bool (bool?)wrapper property in your ViewModel class (I am assuming you are using MVVM pattern) and simply bind your checkbox with that property usingRelativeSource.Put your logic in
getterof that property –And whenever you want the property to be updated on your UI, raise property changed event on your property. (
INotifyProeprtyChangedevent should be implemented by your ViewModel class)