专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

WPF中ComboBox使用

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/2/14 0:14:53       共计:3614 浏览

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>

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:手机访问PC网站的用户就会自动跳转到微官网 | ·下一条:WPF中ComboBox用法

Copyright © 广州京杭网络科技有限公司 2005-2025 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有