简体   繁体   English

WPF组合框样式

[英]WPF Combo Box Style

I have two combo boxes: CarTypeComboBox and SeriesComboBox . 我有两个组合框: CarTypeComboBoxSeriesComboBox

Issues: 1. I want the SeriesCombox to be visible only when the user select BMW . 问题:1.我希望SeriesCombox仅在用户选择BMW可见 2. System.Windows.Style is showing up in SeriesComboBox . 2. System.Windows.Style显示在SeriesComboBox中

Thank you 谢谢

Complete Code : 完整的代码

    <Window x:Class="StyleTrigger.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            xmlns:local="clr-namespace:StyleTrigger"
            xmlns:local2="clr-namespace:ComboBoxData"
            xmlns:sys="clr-namespace:System;assembly=mscorlib"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">


        <Window.Resources >
            <local2:ComboBoxItemCollection x:Key="CarItemsCollection"/>

        </Window.Resources>

        <Grid>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="50*" />
                <ColumnDefinition Width="50*"/>

            </Grid.ColumnDefinitions>


            <Grid.RowDefinitions>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>



            <StackPanel Grid.Row="1" Grid.Column="0" >
                <Label x:Name="CarBrand" Height="30" Width="75" Margin="10,0,0,0"  Content="Car Brand" 
                               HorizontalAlignment="Left" VerticalAlignment="Top"/>
                <ComboBox x:Name="CarTypeComboBox"  Margin="10,0,0,0" Width="100" Height="30"  HorizontalAlignment="Left" VerticalAlignment="Top"  
                        ItemsSource="{Binding Mode=OneWay, Source={StaticResource CarItemsCollection}}" 
                        DisplayMemberPath="CarType" 
                        SelectedValuePath="CarID" 
                     />
            </StackPanel>


            <StackPanel Grid.Row="1" Grid.Column="1" >
                <Label x:Name="CarSeries" Height="30" Width="75" Margin="10,0,0,0"  Content="Car Series" 
                               HorizontalAlignment="Left" VerticalAlignment="Top"/>


                <ComboBox x:Name="SeriesComboBox"  Margin="10,0,0,0" Width="100" Height="30"  
                          HorizontalAlignment="Left" VerticalAlignment="Top">

                    <sys:String>230</sys:String>
                    <sys:String>280</sys:String>
                    <sys:String>530</sys:String>


                    <Style TargetType="ComboBox">

                        <Setter Property="Visibility" Value="Hidden" />

                        <Style.Triggers>
                            <DataTrigger Binding="{Binding SelectedItem.CarType, ElementName=CarTypeComboBox}" Value="BMW">
                                <Setter Property="Visibility" Value="Visible"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </ComboBox>
            </StackPanel>




        </Grid>
    </Window>

C# C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ComboBoxData
        {
        class SingleComboBoxItem
            {
            public SingleComboBoxItem(int pCarID,String pCarBrand)
                {
                CarID = pCarID;
                CarType = pCarBrand;
                }

            public string CarType { get; set; }
            public int CarID { get; set; }

            }
        }


    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ComboBoxData
        {
        class ComboBoxItemCollection : ObservableCollection<SingleComboBoxItem>
            {
            public ComboBoxItemCollection() : base()
                {
                Add(new SingleComboBoxItem(1,"Honda"));
                Add(new SingleComboBoxItem(2,"Toyota"));
                Add(new SingleComboBoxItem(3,"BMW"));
                Add(new SingleComboBoxItem(4,"Dodge"));
                Add(new SingleComboBoxItem(5,"Lexus"));
                }

            }
    }

Style本身 (而不是触发器)添加一个默认值为“ Hidden的附加Setter

Your ComboBox style should look like this: 您的ComboBox样式应如下所示:

<Style TargetType="ComboBox">
  <!-- Just add this one Setter -->
  <Setter Property="Visibility" Value="Hidden" />

  <Style.Triggers>
    <DataTrigger Binding="{Binding SelectedItem.CarType, ElementName=CarTypeComboBox}" Value="BMW">
      <Setter Property="Visibility" Value="Visible"/>
    </DataTrigger>
  </Style.Triggers>
</Style>

Do not set the Visibility attribute on the ComboBox tag itself. 不要在ComboBox标签本身上设置Visibility属性。 No Visibility="Hidden" on the ComboBox . ComboBox上没有Visibility="Hidden" That will override the style setters and it will never be visible. 这将覆盖样式设置器,并且将永远不可见。 ONLY set Visibility in setters in the style. 仅在样式的设置者中设置Visibility

UPDATE 更新

Now that I've seen the whole code, I can offer a little more insight. 现在,我已经看了完整的代码,可以提供更多的见解。 First, you said "The SeriesComboBox is not appearing when I select BMW.", but what's happening in the version you just posted is that it is not disappearing when you don't select BMW. 首先,您说“当我选择BMW时,没有显示SeriesComboBox。”但是,刚发布的版本中发生的事情是,当您选择BMW时,它并没有消失 Now, let's take a look at what it does do: 现在,让我们看一下它的作用:

在此处输入图片说明

There's an anomaly in that dropdown list: The last item is System.Windows.Style . 该下拉列表中有一个异常:最后一项是System.Windows.Style I'm willing to bet you haven't ever seen that model of BMW on the road any more than I have. 我敢打赌,您从未像现在这样在路上看到过宝马这种车型。

Your Style is correctly defined, and I think it may have been correct even before we started hassling you about it. 您的Style已正确定义,我认为即使在我们开始为您争论之前,它也可能是正确的。 The trouble is you aren't assigning it to the Style property of the ComboBox . 问题是您没有将其分配给ComboBoxStyle属性。 Instead you're adding it to the default content property, which in the case of ComboBox is Items . 而是将其添加到默认的content属性,在ComboBox的情况下为Items In WPF, you can throw literally almost figuratively just about anything at all in a ComboBox (or ListBox ) items collection. 在WPF中,您几乎可以比喻地将ComboBox (或ListBox )项目集合中的所有内容扔掉。 It's a collection of object . 这是object的集合。 It'll eat any old garbage you feed it and never complain. 它会吃掉您喂入的所有旧垃圾,永远不会抱怨。 Since you didn't specify DisplayMemberPath for that one, it just cheerfully calls ToString() on each object in turn. 由于您没有为该对象指定DisplayMemberPath ,因此它只是依次对每个对象调用ToString()

So to assign the style to the Style property of the ComboBox , put it inside <ComboBox.Style> : 因此,要将样式分配给ComboBoxStyle属性,请将其放在<ComboBox.Style>

<ComboBox 
    x:Name="SeriesComboBox"  
    Margin="10,0,0,0" 
    Width="100" 
    Height="30"  
    HorizontalAlignment="Left" 
    VerticalAlignment="Top">

    <sys:String>230</sys:String>
    <sys:String>280</sys:String>
    <sys:String>530</sys:String>

    <ComboBox.Style>
        <Style TargetType="ComboBox">
            <Setter Property="Visibility" Value="Hidden" />

            <Style.Triggers>
                <DataTrigger 
                    Binding="{Binding SelectedItem.CarType, ElementName=CarTypeComboBox}" 
                    Value="BMW"
                    >
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
</ComboBox>

You could also define the Style in Resources , give it x:Key="SeriesComboBoxStyle" , and set the Style attribute on the ComboBox tag: Style="{StaticResource SeriesComboBoxStyle}" . 您还可以在Resources定义Style ,将其x:Key="SeriesComboBoxStyle"x:Key="SeriesComboBoxStyle" ,然后在ComboBox标签上设置Style属性: Style="{StaticResource SeriesComboBoxStyle}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM