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>

转自:https://www.cnblogs.com/zhouhb/p/3418103.html

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值