我有一个包含两个ComboBox的表单。第二个ComboBox包含一个客户列表。第一个ComboBox允许用户选择他们想要搜索客户的方式并查看它们。目前,有人让它为每种搜索类型运行不同的存储过程,只是为了改变它在下拉列表中的显示方式。我想改变它,以便它根据第一个下拉列表的选择选择一个DataTemplate。
E.G。如果您从第一个ComboBox中选择First / Last,则客户将在第二个ComboBox下拉列表中显示为:
John Doe
1234假圣乔德勒,阿兹
(480)555-2342
如果您将其更改为Last / First,则客户将在下拉菜单中显示为:
Doe,John 1234假圣乔德勒,阿兹 (480)555-2342
或者如果您选择电子邮件,它将显示为:
JDoe@gmail.com
约翰·多伊
1234 Fake St. Chandler,Az
我知道如何编写模板,但是如何根据第一个ComboBox的选择设置第二个ComboBox.ItemTemplate?我可以使用Triggers或C#代码。
编辑:这是我刚试过的一次尝试,但模板没有改变。我知道触发器正在工作,因为背景变为绿色。<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>
答案 0 :(得分:0)
您可以使用ItemTemplateSelector。它将根据第一个ComboBox中的选择为您选择合适的模板。
答案 1 :(得分:0)
您可以像这样使用DataTrigger
:
<Style x:Key="ComboBox2Style">
<Setter Property="ItemTemplate" Value="{StaticResource DefaultTemplate}" />
<Style.Triggers>
<DataTrigger Binding="{Binding SelectedValue, ElementName=ComboBox1}" Value="LastFirst">
<Setter Property="ItemTemplate" Value="{StaticResource LastNameFirstTemplate}" />
</DataTrigger>
<DataTrigger Binding="{Binding SelectedValue, ElementName=ComboBox1}" Value="Email">
<Setter Property="ItemTemplate" Value="{StaticResource EmailTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
修改强>
刚刚注意到你的问题的编辑。您所拥有的问题是ItemTemplate
标记中定义了<ComboBox />
。根据WPF的Dependency Property Precedence规则,标签中直接定义的值将覆盖任何Styled或Triggered值。要使Trigged模板生效,请在ComboBox的ItemTemplate
Style