導入
第6章で作ったRemainingCount(=>の計算プロパティ)を、実際に画面へ表示してみましょう。計算プロパティは特別な設定なしに、TextBlockから普通のプロパティと同じように{Binding}できます。
説明
classDiagram
class TaskListViewModel {
+Tasks : ObservableCollection~TaskDto~
+RemainingCount : int «=> Tasks.Count(t => !t.IsCompleted)»
}
class TextBlock {
Text="{Binding RemainingCount}"
}
TextBlock ..> TaskListViewModel : 表示のたびに再評価
<Window Title="タスク一覧" Width="360" Height="420">
<StackPanel Margin="16">
<TextBlock Text="残りタスク: " FontWeight="Bold" />
<TextBlock Text="{Binding RemainingCount}" FontSize="20" Foreground="Teal" />
<TextBox Text="{Binding NewTaskTitle, Mode=TwoWay}" Margin="0,10" />
<Button Content="追加" Command="{Binding AddCommand}" Width="80" />
<ListBox ItemsSource="{Binding Tasks}" DisplayMemberPath="Title" Height="120" Margin="0,10" />
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
<Button Content="先頭を完了" Command="{Binding CompleteCommand}" Width="100" Margin="0,0,8,0" />
<Button Content="すべて削除" Command="{Binding DeleteAllCommand}" Width="100" />
</StackPanel>
</StackPanel>
</Window>
public record TaskDto(string Title, bool IsCompleted);
public class TaskListViewModel
{
public string NewTaskTitle { get; set; } = "";
public ObservableCollection<TaskDto> Tasks { get; set; } = new();
public int RemainingCount => Tasks.Count(t => !t.IsCompleted);
public ICommand AddCommand => new RelayCommand(
() => { Tasks.Add(new TaskDto(NewTaskTitle, false)); NewTaskTitle = ""; },
() => !string.IsNullOrWhiteSpace(NewTaskTitle)
);
public ICommand CompleteCommand => new RelayCommand(
() => { Tasks.RemoveAt(0); },
() => Tasks.Any()
);
public ICommand DeleteAllCommand => new RelayCommand(
() => { Tasks.Clear(); },
() => Tasks.Any()
);
}
RemainingCountは保存された値ではなく=>の計算プロパティ。Tasksが変わるたびに、画面が再描画されるタイミングで式が再評価されるTextBlockはText="{Binding RemainingCount}"と書くだけで、普通のプロパティと同じように計算プロパティを表示できる- このランナーの制約上、追加したタスクの
IsCompletedは常にfalseのままなので(要素ごとの書き換え手段がないため)、実質的にRemainingCountはTasks.Countと同じ値になる。式の形自体は第6章の本物のロジックと同じであることに注目してほしい
やってみよう
- 一覧が空の状態で「残りタスク: 0」と表示されることを確認する
- タスクを追加するたびに数字が増えることを確認する
- 「先頭を完了」や「すべて削除」で一覧を減らすと、数字も連動して減ることを確認する
確認ポイント
RemainingCountがTasksの増減に連動して自動的に再計算されること- 計算プロパティも
TextBlockから普通の{Binding}で表示できること
まとめ
- 計算プロパティ(
=>)は保存せず、参照されるたびに再計算される - Viewからは、状態プロパティも計算プロパティも同じ
{Binding}で扱える - ここまでで
TaskListViewModelの主要な機能(一覧・追加・完了/削除・件数表示)がすべてWPFの画面につながった