回答編集履歴

1

見直しキャンペーン中

2023/07/27 15:16

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,179 +1,89 @@
1
1
  > しかし、SelectedIndexをバインドして-1を入れても選択が解除されません。
2
-
3
-
4
2
 
5
3
  今`SelectedIndex`が変わっている最中に、さらに変えてもダメってことじゃないでしょうか(どこかにドキュメントがありそうですが探していません^^;
6
4
 
7
-
8
-
9
5
  -1を入れるのを少し遅らせると反映されます(なんか気持ちが悪いですが^^;
10
-
11
-
12
6
 
13
7
  > リストの項目をボタンのように扱う別の方法でも構いません。
14
8
 
15
-
16
-
17
9
  選択に意味はなく単にボタンを並べるという使い方なら、`ItemsControl`でいいと思います。
18
-
19
-
20
-
21
10
  [ItemsControl 攻略 ~ 外観のカスタマイズ | grabacr.nét](http://grabacr.net/archives/1240)
22
11
 
23
-
24
-
25
- ```xaml
12
+ ```xml
26
-
27
13
  <Window
28
-
29
14
  x:Class="Questions343230.Views.MainWindow"
30
-
31
15
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
32
-
33
16
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
34
-
35
17
  xmlns:prism="http://prismlibrary.com/"
36
-
37
18
  Width="525"
38
-
39
19
  Height="350"
40
-
41
20
  prism:ViewModelLocator.AutoWireViewModel="True">
42
-
43
21
  <Grid>
44
-
45
22
  <Grid.ColumnDefinitions>
46
-
47
23
  <ColumnDefinition />
48
-
49
24
  <ColumnDefinition />
50
-
51
25
  </Grid.ColumnDefinitions>
52
26
 
53
-
54
-
55
27
  <GroupBox Header="ListBox">
56
-
57
28
  <ListBox ItemsSource="{Binding Items}" SelectedIndex="{Binding Index.Value}" />
58
-
59
29
  </GroupBox>
60
30
 
61
-
62
-
63
31
  <GroupBox Grid.Column="1" Header="ItemsControl">
64
-
65
32
  <ItemsControl ItemsSource="{Binding Items}">
66
-
67
33
  <ItemsControl.ItemTemplate>
68
-
69
34
  <DataTemplate>
70
-
71
35
  <Button
72
-
73
36
  Command="{Binding DataContext.Command, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"
74
-
75
37
  CommandParameter="{Binding}"
76
-
77
38
  Content="{Binding}" />
78
-
79
39
  </DataTemplate>
80
-
81
40
  </ItemsControl.ItemTemplate>
82
-
83
41
  </ItemsControl>
84
-
85
42
  </GroupBox>
86
-
87
43
  </Grid>
88
-
89
44
  </Window>
90
-
91
45
  ```
92
46
 
93
-
94
-
95
- ```C#
47
+ ```cs
96
-
97
48
  using Prism.Mvvm;
98
-
99
49
  using Reactive.Bindings;
100
-
101
50
  using System;
102
-
103
51
  using System.Collections.Generic;
104
-
105
52
  using System.Diagnostics;
106
-
107
53
  using System.Reactive.Linq;
108
-
109
54
  using System.Threading.Tasks;
110
55
 
111
-
112
-
113
56
  namespace Questions343230.ViewModels
114
-
115
57
  {
116
-
117
58
  public class MainWindowViewModel : BindableBase
118
-
119
59
  {
120
-
121
60
  public List<string> Items { get; } = new List<string> { "あああ", "いいい", "ううう", "えええ", "おおお", };
122
-
123
61
  public ReactivePropertySlim<int> Index { get; } = new ReactivePropertySlim<int>();
124
-
125
62
  public ReactiveCommand<string> Command { get; } = new ReactiveCommand<string>();
126
63
 
127
-
128
-
129
64
  public MainWindowViewModel()
130
-
131
65
  {
132
-
133
66
  Index.Subscribe(OnIndexChange);
134
-
135
67
  Command.Subscribe(x => Debug.WriteLine(x));
136
-
137
68
  }
138
69
 
139
-
140
-
141
70
  // fire & forget
142
-
143
71
  private async void OnIndexChange(int x)
144
-
145
72
  {
146
-
147
73
  if (x == -1) return;
148
-
149
74
  Debug.WriteLine(x);
150
75
 
151
-
152
-
153
76
  // わかりやすいように1秒待つ
154
-
155
77
  await Task.Delay(1000);
156
78
 
157
-
158
-
159
79
  // 選択時のハイライトがちょうどクリック感があっていい感じw
160
-
161
80
  //await Task.Delay(100);
162
81
 
163
-
164
-
165
82
  // おそらくこれでもいい気がするが、根拠はない
166
-
167
83
  //await Task.Delay(1);
168
84
 
169
-
170
-
171
85
  Index.Value = -1;
172
-
173
86
  }
174
-
175
87
  }
176
-
177
88
  }
178
-
179
89
  ```