WPF (XAML) を使用してウィンドウのオプションを変更するたびに、Resfresh() または Work() メソッドを呼び出す方法を理解しようとしています。すでに質問していますが、十分に明確ではありませんでした。ですから、より良い例でもう一度質問します。
多くのビジュアル コンポーネントからラベルを更新する方法を知りたいです。0 から 9 までのラベルが付いた 10 個のチェックボックスがあり、チェックされている場合はそれらの合計を計算したいとします。
従来の Winform では、イベント ハンドラー OnClick() を作成し、CheckBox の状態が変化するたびにイベントを呼び出します。OnClick は Refresh() グローバル メソッドを呼び出します。Refresh は、各 CheckBox がチェックされているかどうかを評価し、必要に応じてそれらを合計します。Refresh() メソッドの最後で、Label Text プロパティを合計に設定します。
XAML とデータ バインディングを使用してそれを行うにはどうすればよいですか?
<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}"/>
私のViewModelには、チェックボックスごとにデータバインドされたプロパティがあり、合計用に1つあります
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();
}
私の質問は、この Refresh メソッドをいつどのように呼び出す必要があるかです。