回答編集履歴

2

見直しキャンペーン中

2023/07/17 13:41

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,4 +1,4 @@
1
- ComboBoxなので、選択肢を`ItemsSource`にBindingする必要があります。
1
+ ComboBoxなので、**選択肢**を`ItemsSource`にBindingする必要があります。
2
2
  Categoryは`SelectedItem`にあたります。
3
3
 
4
4
  変更した点のみ記載します。

1

見直しキャンペーン中

2023/07/17 13:40

投稿

TN8001
TN8001

スコア9350

test CHANGED
@@ -1,65 +1,33 @@
1
- ComboBoxなので選択肢を`ItemsSource`にBindingする必要があります。
1
+ ComboBoxなので選択肢を`ItemsSource`にBindingする必要があります。
2
-
3
2
  Categoryは`SelectedItem`にあたります。
4
3
 
5
-
6
-
7
4
  変更した点のみ記載します。
8
-
9
- ```xaml
5
+ ```xml
10
-
11
6
  <DataTemplate>
12
-
13
7
  <ComboBox
14
-
15
8
  DisplayMemberPath="DisplayValue"
16
-
17
9
  ItemsSource="{Binding CategoryTypes}"
18
-
19
10
  SelectedItem="{Binding Category, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
20
-
21
11
  <!--<ComboBox.ItemsSource>
22
-
23
12
  <x:Array Type="{x:Type viewModels:CategoryType}">
24
-
25
13
  <x:Static Member="viewModels:CategoryType.None" />
26
-
27
14
  <x:Static Member="viewModels:CategoryType.Category1" />
28
-
29
15
  <x:Static Member="viewModels:CategoryType.Category2" />
30
-
31
16
  </x:Array>
32
-
33
17
  </ComboBox.ItemsSource>-->
34
-
35
18
  </ComboBox>
36
-
37
19
  </DataTemplate>
38
-
39
20
  ```
40
21
 
41
-
42
-
43
- ```C#
22
+ ```cs
44
-
45
23
  public class Item : BindableBase
46
-
47
24
  {
48
-
49
25
  public string Name { get; set; }
50
-
51
26
  private CategoryType _category;
52
-
53
27
  public CategoryType Category { get => _category; set => SetProperty(ref _category, value); }
54
28
 
55
-
56
-
57
29
  // なんだかなって気もするのでxamlで与えたほうがいいか?
58
-
59
30
  // xamlで指定する場合は入れ子だとエラーになるのでclass CategoryTypeを外に出す必要あり
60
-
61
31
  public static List<CategoryType> CategoryTypes { get; } = new List<CategoryType> { CategoryType.None, CategoryType.Category1, CategoryType.Category2 };
62
-
63
32
  }
64
-
65
33
  ```