WPF组合框样式

时间:2016-06-29 18:51:18

标签: wpf combobox styles datatrigger

我有两个组合框: CarTypeComboBox SeriesComboBox

问题: 1.只有当用户选择 BMW 时,我才希望 SeriesCombox 可见。 2. System.Windows.Style显示在 SeriesComboBox 中。

谢谢

完整代码

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

    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"));
                }

            }
    }

2 个答案:

答案 0 :(得分:1)

Setter本身添加一个默认值Hidden 的其他Style,而非触发器。

答案 1 :(得分:0)

您的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>

请勿在{{1​​}}标记本身上设置Visibility属性。 ComboBox上没有Visibility="Hidden"。这将覆盖样式设置器,它将永远不会可见。只在样式的setter中设置ComboBox

更新

现在我已经看过整个代码,我可以提供更多的见解。首先,你说“我选择宝马时不会出现SeriesComboBox。”但是你发布的版本中发生的事情是当你不消失 >选择宝马。现在,我们来看看它的作用:

enter image description here

该下拉列表中存在异常:最后一项是Visibility。我愿意打赌你没有在路上看到过宝马这款车型。

您的System.Windows.Style已正确定义,我认为即使在我们开始讨论您之前它也可能是正确的。问题是您没有将其分配给Style的{​​{1}}属性。相反,您将其添加到默认内容属性,在Style的情况下为ComboBox。在WPF中,您可以在ComboBox(或Items)项集合中完全按照几乎所有的形式抛出。它是ComboBox的集合。它会吃掉你喂它的任何旧垃圾而不会抱怨。由于您没有为该帐户指定ListBox,因此它只是依次高兴地调用每个对象上的object

所以要将样式分配到DisplayMemberPath的{​​{1}}属性,请将其放在ToString()内:

Style

您还可以在ComboBox中定义<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> ,并在Style代码上设置Resources属性:{{1} }。