I'm trying to understand how I can call a Resfresh() ou Work() method each time I modify an option on a window with WPF (XAML). I already ask the question but I wasn't clear enough. So I will ask again with a better example.
I would like to know how I can update a label from many visual component. Let say we have 10 checkbox with label 0 to 9 and I would like to do the sum of them if they are checked.
In classic Winform I'll create an event handler OnClick() and call the event on each CheckBox state change. OnClick call a Refresh() global method. Refresh evaluate if each CheckBox is checked and sum them if required. At the end of the Refresh() method I set the Label Text property to my sum.
How can I do that with XAML and data binding ?
<CheckBox Content="0" Name="checkBox0" ... IsChecked="{Binding Number0}" />
<CheckBox Content="1" Name="checkBox1" ... IsChecked="{Binding Number1}" />
<CheckBox Content="2" Name="checkBox2" ... IsChecked="{Binding Number2}" />
<CheckBox Content="3" Name="checkBox3" ... IsChecked="{Binding Number3}" />
<CheckBox Content="4" Name="checkBox4" ... IsChecked="{Binding Number4}" />
...
<Label Name="label1" ... Content="{Binding Sum}"/>
In my ViewModel I have a data binded property for each checkBox and one for the Sum
private bool number0;
public bool Number0
{
get { return number0; }
set
{
number0 = value;
NotifyPropertyChanged("Number0");
// Should I notify something else here or call a refresh method?
// I would like to create something like a global NotifyPropertyChanged("Number")
// But how can I handle "Number" ???
}
}
// Same for numer 1 to 9 ...
private bool sum;
public bool Sum
{
get { return sum; }
set
{
sum = value;
NotifyPropertyChanged("Sum");
}
}
private void Refresh() // or Work()
{
int result = 0;
if (Number0)
result = result + 0; // Could be more complex that just addition
if (Number1)
result = result + 1; // Could be more complex that just addition
// Same until 9 ...
Sum = result.ToString();
}
My question is how and when should I call this Refresh method?