0

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>
4

2 に答える 2

0

ItemTemplateSelectorを使用できます。最初の ComboBox での選択に基づいて、適切なテンプレートが選択されます。

于 2011-11-04T19:32:04.300 に答える
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 テンプレートを有効にするにItemTemplateは、ComboBox のデフォルトを設定しますStyle

于 2011-11-04T19:34:28.023 に答える