• WPF ComboBox(转)


    WPF ComboBox

    创建一个ComboBox控件,并设置ComboBox控件的名称,高度,宽度。及设置ComboBox的垂直和水平对齐。

    <ComboBox Name="ComboBox1" Width="200" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,10,0,0"></ComboBox>

    输出结果如图所示:

    添加ComboBox项

    <ComboBox Name="ComboBox1" Width="200" Height="30" VerticalAlignment="Top" HorizontalAlignment="Left" Margin="10,10,0,0">
    <ComboBoxItem Content="太原"></ComboBoxItem>
    <ComboBoxItem Content="北京" IsSelected="True"></ComboBoxItem>
    <ComboBoxItem Content="石家庄"></ComboBoxItem>
    <ComboBoxItem Content="哈尔滨"></ComboBoxItem>
    <ComboBoxItem Content="合肥"></ComboBoxItem>
    <ComboBoxItem Content="南京"></ComboBoxItem>
    </ComboBox>

    IsSelected属性为ComboxBox中的默认选中项

    输出结果如图所示:

    在运行时添加和删除ComboBox项

    XAML

    <StackPanel Orientation="Horizontal" Height="40" VerticalAlignment="Top">
    <ComboBox Name="ComboBox1" Width="100" Margin="5" VerticalContentAlignment="Center">
    <ComboBoxItem Content="太原"></ComboBoxItem>
    <ComboBoxItem Content="北京" ></ComboBoxItem>
    <ComboBoxItem Content="石家庄"></ComboBoxItem>
    <ComboBoxItem Content="哈尔滨"></ComboBoxItem>
    <ComboBoxItem Content="合肥"></ComboBoxItem>
    <ComboBoxItem Content="南京"></ComboBoxItem>
    </ComboBox>
    <TextBox Width="100" Margin="5" x:Name="ItemNames" VerticalContentAlignment="Center"></TextBox>
    <Button Margin="5" Content="添加" x:Name="Add" Width="60" Click="Add_Click"></Button>
    <Button Margin="5" Content="删除" x:Name="Delete" Width="60" Click="Delete_Click"></Button>
    </StackPanel>

    CS

    private void Add_Click(object sender, RoutedEventArgs e)
    {
    if(!string.IsNullOrEmpty(ItemNames.Text))
    ComboBox1.Items.Add(ItemNames.Text);
    }

    private void Delete_Click(object sender, RoutedEventArgs e)
    {
    ComboBox1.Items.RemoveAt(ComboBox1.Items.IndexOf(ComboBox1.SelectedItem));
    }

    输出结果如图所示:

    修改ComboBoxItem样式

    <ComboBox Name="ComboBox1" Width="100" Margin="5" VerticalContentAlignment="Center">
    <ComboBoxItem Content="太原" Background="LightGray" Foreground="Black" FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ComboBoxItem>
    <ComboBoxItem Content="北京" Background="LightBlue" Foreground="Purple" FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ComboBoxItem>
    <ComboBoxItem Content="石家庄" Background="LightGreen" Foreground="Green" FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ComboBoxItem>
    <ComboBoxItem Content="哈尔滨" Background="LightBlue" Foreground="Blue" FontFamily="Verdana" FontSize="12" FontWeight="Bold"></ComboBoxItem>
    <ComboBoxItem Content="合肥" Background="LightSlateGray" Foreground="Orange" FontFamily="Georgia" FontSize="14" FontWeight="Bold"></ComboBoxItem>
    </ComboBox>

    输出结果如图所示:

    在ComboBoxItem项中显示图片

    <ComboBoxItem Background="LightGray" Foreground="Black" FontFamily="Georgia" FontSize="14" FontWeight="Bold">
    <StackPanel Orientation="Horizontal">
    <Image Source="Image12.jpg" Height="30" Width="30"></Image>
    <TextBlock Text="太原" VerticalAlignment="Center"></TextBlock>
    </StackPanel>
    </ComboBoxItem>

    输出结果如图所示:

    将复选框添加到ComboBoxItem

    <ComboBoxItem Background="LightGray" Foreground="Black" FontFamily="Georgia" FontSize="14" FontWeight="Bold">
    <CheckBox Name="TaiYuanCheckBox">
    <StackPanel Orientation="Horizontal">
    <Image Source="Image12.jpg" Height="30" Width="30"></Image>
    <TextBlock Text="太原" VerticalAlignment="Center"></TextBlock>
    </StackPanel>
    </CheckBox>
    </ComboBoxItem>

    输出结果如图所示:

    获取当前选定项的值

    string str= ComboBox1.SelectedItem.ToString();

     

  • 相关阅读:
    C# 2010 从入门到精通 学习笔记3 第4章 使用决策语句
    C# 2010 从入门到精通 学习笔记2 第3章 方法和作用域
    C# 2010 从入门到精通 学习笔记1 第2章 使用变量、操作符和表达式
    SharePoint 2010 添加“我的链接”菜单
    SharePoint Survey WebPart 调查 Web部件
    SharePoint World Clock 世界时钟
    SharePoint Silverlight Clock 时钟
    SharePoint 文档导入工具
    如何在SharePoint2010中添加Deep Zoom Image
    计算并发和qps:
  • 原文地址:https://www.cnblogs.com/ExMan/p/3654213.html
Copyright © 2020-2023  润新知