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>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM