1. 程式人生 > >WPF學習筆記 ComboBox的資料繫結

WPF學習筆記 ComboBox的資料繫結

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow

也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!

               

UI

<UserControl x:Class="UnitViews.UserMeUV"             xmlns
="http://schemas.microsoft.com/winfx/2006/xaml/presentation"             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"              mc:Ignorable="d"              xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>        <ComboBox x:Name="cbStatus" ItemsSource="{Binding StatusList}"                   SelectedValuePath="Key" DisplayMemberPath="Value" SelectedItem="{Binding SelectedStatus}"                  Height="30">            <i:Interaction.Triggers>                <i:EventTrigger EventName="SelectionChanged">                    <i:InvokeCommandAction Command="{Binding SelectionChangedCmd}"                             CommandParameter="{Binding ElementName=cbStatus}" />                </i:EventTrigger>            </i:Interaction.Triggers>                    </ComboBox>    </Grid></UserControl>

VM

        static Dictionary<User.EStatus, string> olist = new Dictionary<User.EStatus, string>         {             {1,"有錢" }            ,{2,"有閒" }            ,{3,"有料" }            ,{4,"有鬼" }        };        public Dictionary<User.EStatus, string> StatusList        {            get            {                return olist;            }        }        KeyValuePair<User.EStatus, string> _kvp;        public KeyValuePair<User.EStatus, string> SelectedStatus        {            get            {                return this._kvp;            }            set            {                this._kvp = value;                this.RaisePropertyChanged("SelectedStatus");            }        }        DelegateCommand<ComboBox> _SelectionChangedCmd = null;        public DelegateCommand<ComboBox> SelectionChangedCmd        {            get            {                if (this._SelectionChangedCmd == null)                {                    this._SelectionChangedCmd = new DelegateCommand<ComboBox>(SelectionChanged);                }                return this._SelectionChangedCmd;            }        }        void SelectionChanged(ComboBox cb)        {            SelectedStatus = (KeyValuePair<User.EStatus, string>)cb.SelectedItem;            Status = SelectedStatus.Key;        }        public User.EStatus Status        {            set            {                var query = olist.Where(n => n.Key == value);                if (query != null && query.Count() > 0)                {                    this.SelectedStatus = query.First();                }            }        }

注意裡面用了Dictionary<>作為資料來源的型別,因此有ComboBox控制元件裡有

SelectedValuePath="Key" DisplayMemberPath="Value"

參考文章:

http://www.luacloud.com/2011/wpf-combobox-binding-data.html

           

給我老師的人工智慧教程打call!http://blog.csdn.net/jiangjunshow

這裡寫圖片描述