zoukankan      html  css  js  c++  java
  • WPF中ComboBox使用

    1、数据绑定

    前台代码:

            <ComboBox Height="23" HorizontalAlignment="Left" Margin="86,143,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120">
            </ComboBox>

    后台代码:

          

        class ProductImg   //声明类
        {
            int id;

            public int Id
            {
                get { return id; }
                set { id = value; }
            }
            string img;

            public string Img
            {
                get { return img; }
                set { img = value; }
            }
        }

            ObservableCollection<ProductImg> imgs = new ObservableCollection<ProductImg>();   //集合,即数据源

             comboBox1.SelectedValuePath = "Id";   //程序内部维护的值
             comboBox1.DisplayMemberPath = "Img";  //显示的内容
             comboBox1.ItemsSource = imgs;  //数据源
             comboBox1.SelectedValue = 3;  //选中的值

    2、在ComboBox中显示图像

    代码:

    <ComboBox Height="33" HorizontalAlignment="Right" Margin="0,94,31,0" x:Name="comboBox1" VerticalAlignment="Top" Width="142" SelectedIndex="0">
        <ComboBoxItem>
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/roles.png" Height="30" />
                <TextBlock Text="Select a role" />
            </StackPanel>
        </ComboBoxItem>
        <ComboBoxItem Background="LightCoral">
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/cashier.gif" Height="30" />
                <TextBlock Text="Cashier" />
            </StackPanel>
        </ComboBoxItem>
        <ComboBoxItem Background="LightGreen">
            <StackPanel Orientation="Horizontal">
                <Image Source="Images/manager.gif" Height="30" />
                <TextBlock Text="Manager" />
            </StackPanel>
        </ComboBoxItem>
    </ComboBox>

     

  • 相关阅读:
    2019中山纪念中学夏令营-Day19 数论初步【GCD(最大公约数),素数相关】
    2019中山纪念中学夏令营-Day14 图论初步【dijkstra算法求最短路】
    2019中山纪念中学夏令营-Day12[JZOJ]
    2019中山纪念中学夏令营-Day9[JZOJ](第六次模拟赛)
    2019中山纪念中学夏令营-Day4[JZOJ]
    2019中山纪念中学夏令营-Day2[JZOJ]
    2019中山纪念中学夏令营-Day1[JZOJ]
    CCPC2019江西省赛-Problem G.Traffic
    T137223 节能主义
    T137226 彩虹海
  • 原文地址:https://www.cnblogs.com/zhouhb/p/3418103.html
Copyright © 2011-2022 走看看