WPF 自定义带有数据源的combobox控件

来源:互联网 发布:开淘宝网店要多少钱? 编辑:程序博客网 时间:2024/05/16 09:39

1  自定义控件的代码

Public Class myCombobox    Inherits ComboBox    Public obs As List(Of MODEL.clstblColorMaster)    Sub New()        obs = BLL.BL_clstblColorMaster.FillData        Me.ItemsSource = obs        Me.DisplayMemberPath = "ColorCode"        Me.SelectedValuePath = "ColorCode"    End SubEnd Class

2 在窗体中引用的代码

<Window    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:VIEWMODEL="clr-namespace:WpfApplication1.VIEWMODEL" xmlns:local="clr-namespace:WpfApplication1" mc:Ignorable="d" x:Class="MainWindow"    Title="MainWindow" Height="800" Width="800">    <Grid >    <local:myCombobox SelectedValue="{Binding CurrentItem.ColorCode}"  Background="Red"  HorizontalAlignment="Left" Margin="126,82,0,0" Grid.Row="5" VerticalAlignment="Top" Width="144"/>    </Grid></Window>


0 0