Fork me on GitHub

ComboBox

       传统的ComboBox功能在WPF中的ComboBox没有多在改变,如下例中的第一个ComboBox。它用Trigger实了变化,同时触发了一个OnHover事件,使ComboBox一个子项改变内容。与传统ComboBox不一样的是第二个ComboBox,在它的子项中出现了image控件,Button控件和TextBox控件,就是说WPF能很容易的很用控件嵌套控件,这里的XAML语言的优点就能显而易见了。在ComboBox这个控件的子项中的控件,仍然保留着自己的事件的属性,如下例中第二个按钮的单击事件。

XAML代码

<Page

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       x:Class="Test.combox1"

       x:Name="Page"

       WindowTitle="Page"

       FlowDirection="LeftToRight"

       Width="640" Height="480"

       WindowWidth="640" WindowHeight="480">

<Page.Resources>

 <Style x:Key="Triggers" TargetType="{x:Type ComboBoxItem}">

 <Style.Triggers>

 <Trigger Property="ComboBoxItem.IsMouseOver" Value="true">

     <Setter Property = "Foreground" Value="Red"/>

     <Setter Property = "FontSize" Value="18"/>

     <Setter Property = "FontFamily" Value="Arial Bold"/>

 </Trigger>

 </Style.Triggers>

</Style>

</Page.Resources>

       <Grid x:Name="LayoutRoot">

       <ComboBox Text="Is not open"    RenderTransformOrigin="0.5,0.5" HorizontalAlignment="Left" Margin="44,0,0,0" VerticalAlignment="Top" Width="215" Height="25">          

    <ComboBoxItem Style="{StaticResource Triggers}" MouseMove="OnHover" Name="cbi1">Item1</ComboBoxItem>

    <ComboBoxItem Style="{StaticResource Triggers}" MouseMove="OnHover" Name="cbi2">Item2</ComboBoxItem>

    <ComboBoxItem Style="{StaticResource Triggers}" MouseMove="OnHover" Name="cbi3">Item3</ComboBoxItem>

</ComboBox>

<ComboBox Name="cb" Margin="44,151,0,225" HorizontalAlignment="Left" Width="200">

        <ComboBoxItem Style="{StaticResource Triggers}">Spain - Item 0</ComboBoxItem>

        <ComboBoxItem Style="{StaticResource Triggers}">

            <Image Source="images\apple.jpg" HorizontalAlignment="Left" VerticalAlignment="Bottom" Width="114.667" Height="86"/>

              </ComboBoxItem >

        <ComboBoxItem Style="{StaticResource Triggers}">

                     <Button HorizontalAlignment="Left" Click="OnClick" Margin="0,0,0,0" VerticalAlignment="Top" Width="87" Height="20" Content="Button"/>

              </ComboBoxItem>

        <ComboBoxItem Style="{StaticResource Triggers}">

              <TextBox Name="text" Margin="0,0,0,0" VerticalAlignment="Top" Height="24" Text="TextBox" TextWrapping="Wrap"/>

</ComboBoxItem>

</ComboBox>

</Grid>

</Page>

CS代码:

private void OnHover(object sender, RoutedEventArgs e)

        {

            cbi1.Content = "You clicked the check box";

        }

        private void OnClick(object sender, RoutedEventArgs e)

        {

            text.Text = "GSW";

        }


第一个
ComboBox的效果

第二个ComboBox的效果。
本文参考MSDN组织。

posted @ 2007-03-19 11:30  桂素伟  阅读(1729)  评论(0编辑  收藏  举报