teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

追記

2020/03/10 00:39

投稿

lain
lain

スコア161

title CHANGED
File without changes
body CHANGED
@@ -154,4 +154,63 @@
154
154
 
155
155
  Windows 10
156
156
  Visual Studio 2019
157
- .NET Framework 4.8
157
+ .NET Framework 4.8
158
+
159
+
160
+ ### 解決いたしました(以下、解決コード)
161
+ ```C#
162
+ public class PersonTemplateSelector : DataTemplateSelector
163
+ {
164
+ public override DataTemplate SelectTemplate(object item, DependencyObject container)
165
+ {
166
+ ContentPresenter presenter = (ContentPresenter)container;
167
+ if (presenter.TemplatedParent is ComboBox)
168
+ return (DataTemplate)presenter.FindResource("SingleLine");
169
+ else
170
+ return (DataTemplate)presenter.FindResource("MultiLine");
171
+ }
172
+ }
173
+
174
+ ```
175
+
176
+ 上記クラスを追加しまして、
177
+
178
+ ```XAML
179
+ <Window.Resources>
180
+ <DataTemplate x:Key="SingleLine">
181
+ <StackPanel Orientation="Horizontal">
182
+ <TextBlock Text="{Binding ID}"/>
183
+ <TextBlock Text="{Binding Name}" />
184
+ </StackPanel>
185
+ </DataTemplate>
186
+
187
+ <DataTemplate x:Key="MultiLine">
188
+ <Grid>
189
+ <Grid.RowDefinitions>
190
+ <RowDefinition Height="Auto" />
191
+ <RowDefinition Height="Auto" />
192
+ </Grid.RowDefinitions>
193
+ <Grid.ColumnDefinitions>
194
+ <ColumnDefinition Width="Auto" />
195
+ <ColumnDefinition Width="Auto" />
196
+ </Grid.ColumnDefinitions>
197
+
198
+ <TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding ID}"/>
199
+ <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Name}" />
200
+ <TextBlock Grid.Row="1" Grid.Column="0" Text="{Binding LeftData.Data1}" />
201
+ <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding RightData.Data1}" />
202
+ </Grid>
203
+ </DataTemplate>
204
+ </Window.Resources>
205
+ ```
206
+
207
+ リソースにDataTemplateを2つ用意し、
208
+
209
+ ```XAML
210
+ <ComboBox ItemsSource="{Binding PersonList, ElementName=my}"
211
+     SelectedItem="{Binding SelectedPerson, ElementName=my}">
212
+     <ComboBox.ItemTemplateSelector>
213
+         <local:PersonTemplateSelector />
214
+     </ComboBox.ItemTemplateSelector>
215
+ </ComboBox>
216
+ ```

1

説明の追加

2020/03/10 00:39

投稿

lain
lain

スコア161

title CHANGED
File without changes
body CHANGED
@@ -11,6 +11,20 @@
11
11
  選択された状態は一行で表示させたい。
12
12
  上図では出来ていないですが、「0 山田太郎」の一行で表示したいです。
13
13
 
14
+ Personクラス内に下記プロパティをつくり、DisplayMemberPathプロパティにバインドした時は、
15
+ ```
16
+ public string DispStr
17
+ {
18
+ get { return string.Format("{0}{1}", ID, Name); }
19
+ }
20
+ ```
21
+
22
+ System.Windows.Markup.XamlParseException
23
+ InvalidOperationException: DisplayMemberPath と ItemTemplate を両方とも設定できません。
24
+
25
+ というエラーでした。
26
+
27
+
14
28
  ComboBox.SelectionBoxItemTemplateというそれらしいプロパティがあるのですが、使い方がいまいち分からないです。
15
29
 
16
30
  お手数をお掛けしますが、お知恵をお貸しいただければさいわいです。