プロパティを10個用意するのではなく
1つのプロパティを持ったVMのインスタンスを10個用意します。
こんな感じです。
(ReactiveCommand使いましたが、本題とは関係ないので気にしないでください。)
xml
1<StackPanel>
2 <Button Content="Click!" Command="{Binding OkButtonCommand}"/>
3 <TextBlock Text="{Binding PropArray[0].Value}"/>
4 <TextBlock Text="{Binding PropArray[1].Value}"/>
5</StackPanel>
C#
1class MainVM
2{
3 public SomeVM[] PropArray { get; } = new SomeVM[] { new SomeVM(1), new SomeVM(2) };
4 public ReactiveCommand OkButtonCommand { get; }
5 public MainVM()
6 {
7 OkButtonCommand = new ReactiveCommand();
8 OkButtonCommand.Subscribe(_ => SomethingToDo());
9 }
10
11 private void SomethingToDo()
12 {
13 PropArray[0].Value++;
14 }
15}
C#
1class SomeVM : INotifyPropertyChanged
2{
3 public event PropertyChangedEventHandler PropertyChanged;
4 protected void OnPropertyChanged(PropertyChangedEventArgs e)
5 {
6 PropertyChanged?.Invoke(this, e);
7 }
8
9 private int _value;
10 public int Value
11 {
12 get { return _value; }
13 set
14 {
15 if (value == _value)
16 return;
17 _value = value;
18 OnPropertyChanged(new PropertyChangedEventArgs(nameof(Value)));
19 }
20 }
21
22 public SomeVM(int name)
23 {
24 _value = name;
25 }
26}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/23 00:45
2016/09/23 01:01