2 つの ComboBox を持つフォームがあります。2 番目の ComboBox には、顧客のリストが保持されます。最初の ComboBox では、顧客を検索して表示する方法をユーザーが選択できます。現在、ドロップダウンでの表示方法を変更するためだけに、検索の種類ごとに異なるストアド プロシージャを実行している人がいます。最初のドロップダウンの選択に基づいて DataTemplate を選択するように変更したいと思います。
EG 最初の ComboBox から First/Last を選択すると、顧客は 2 番目の ComboBox ドロップダウンに次のように表示されます。
John Doe
1234 Fake St. Chandler, Az
(480) 555-2342
これを Last/First に変更すると、顧客はドロップダウンに次のように表示されます。
Doe, John
1234 Fake St. Chandler, Az
(480) 555-2342
または、メールを選択すると、次のように表示されます。
JDoe@gmail.com
John Doe
1234 フェイク セント チャンドラー、アリゾナ州
テンプレートの書き方は知っていますが、最初の ComboBox の選択に基づいて 2 番目の ComboBox.ItemTemplate を設定するにはどうすればよいですか? Triggers または C# コードを使用しても問題ありません。
編集: これは私が試した 1 つの試みですが、テンプレートは変更されません。背景が緑色に変わるので、トリガーが機能していることがわかります。
<UserControl.Resources>
<DataTemplate x:Key="ComboBoxCustomTemplate">
<Grid Margin="3 3">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="{Binding Name}" />
<WrapPanel Grid.Row="1" Orientation="Horizontal">
<TextBlock Text="Address:" />
<TextBlock HorizontalAlignment="Left" Foreground="#003366" Margin="3,0,0,0" Text="{Binding Address}" />
</WrapPanel>
<WrapPanel Grid.Row="2" Orientation="Horizontal">
<TextBlock Text="Phone:" />
<TextBlock HorizontalAlignment="Left" Foreground="#003366" Margin="3,0,0,0" Text="{Binding Telephone}" />
</WrapPanel>
</Grid>
</DataTemplate>
<DataTemplate x:Key="ComboBoxEmailTemplate">
<Grid Margin="3 3">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.ColumnSpan="2" Text="{Binding Email}" />
<WrapPanel Grid.Row="1" Orientation="Horizontal">
<TextBlock Text="Address:" />
<TextBlock HorizontalAlignment="Left" Foreground="#003366" Margin="3,0,0,0" Text="{Binding Address}" />
</WrapPanel>
<WrapPanel Grid.Row="2" Orientation="Horizontal">
<TextBlock Text="Phone:" />
<TextBlock HorizontalAlignment="Left" Foreground="#003366" Margin="3,0,0,0" Text="{Binding Telephone}" />
</WrapPanel>
</Grid>
</DataTemplate>
</UserControl.Resources>
<ComboBox Name="cbSearchFilter" Padding="5,1" Width="150" Margin="3,3,10,3" SelectionChanged="cbSearchFilter_SelectionChanged" Style="{StaticResource VirtualizingComboBox}">
<ComboBoxItem Content="Parent Last/First" Tag="LastFirst" />
<ComboBoxItem Content="Parent First/Last" Tag="FirstLast" />
<ComboBoxItem Content="Student First/Last" Tag="Student" IsSelected="True" />
<ComboBoxItem Content="Parent Phone Number" Tag="PhoneNumber"/>
<ComboBoxItem Content="Parent Email" Tag="Email"/>
</ComboBox>
<ComboBox Name="cbCustomers"
SelectedValuePath="FamilyID"
ItemTemplate="{StaticResource ComboBoxCustomTemplate}"
Grid.Column="1" Grid.Row="2" IsEditable="True" StaysOpenOnEdit="True"
KeyboardNavigation.IsTabStop="False" SelectionChanged="rcbCustomers_SelectionChanged" KeyUp="rcbCustomers_KeyUp" KeyDown="rcbCustomers_KeyDown" >
<ComboBox.Style>
<Style TargetType="{x:Type ComboBox}" BasedOn="{StaticResource VirtualizingComboBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding ElementName=cbSearchFilter, Path=SelectedItem.Tag}" Value="Email">
<DataTrigger.Setters>
<Setter Property="Background" Value="Green" />
<Setter Property="ItemTemplate" Value="{StaticResource ComboBoxEmailTemplate}" />
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
</ComboBox.Style>
</ComboBox>