WPF的MVVM模式給ComboBox綁定數據和讀取


在網上找到類似的代碼:

XAML文件

<ComboBox Margin="-16,3,0,5" Grid.Row="1" Grid.Column="2" Grid.ColumnSpan="2" Name="cboxLocationKeyword"
 ItemsSource="{Binding LocationSource,Mode=OneWay}"
 SelectedValuePath="ID"
 DisplayMemberPath="Info"
 SelectedItem="{Binding SelectLocation}" />

對應的ViewModel文件

public class LocationRoad
{
    public int ID { set; get; }
    public string Code { set; get; }
    public string Info { set; get; }
}
//
/// 當ComboBox選中項更改時發生
///
private LocationRoad _selectLocation;
public LocationRoad SelectLocation
{
    get
    {
        return this._selectLocation;
    }
    set
    {
        this._selectLocation = value;
        if (this.PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("SelectLocation"));
    }
}
 
private ObservableCollection _locationRoad = null;
 
public ObservableCollection LocationSource
{
    get
    {
        if (this._locationRoad == null)
        {
            this._locationRoad = new ObservableCollection() {
                 new LocationRoad() { ID = 1, Code = "NGQ", Info = "南崗區" },
                 new LocationRoad() { ID = 2, Code = "DLQ", Info = "道里區" },
                 new LocationRoad() { ID = 3, Code = "DWQ", Info = "道外區" },
                 new LocationRoad() { ID = 4, Code = "PFQ", Info = "平房區" },
                 new LocationRoad() { ID = 5, Code = "XFQ", Info = "香坊區" },
                 };
 
        }
        return this._locationRoad;
    }
    set
    {
        this._locationRoad = value;
        if (this.PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs("LocationSource"));
    }
}

如果要進行雙向綁定或其他的綁定方式,只要更改上面binging塊中的Mode方式就可以了。一般用不到吧?

 

 

出處:https://bbs.csdn.net/topics/390091439

============================================

我根據上面的代碼,做了一點修改

<ComboBox Name="CbClientType" SelectedIndex="0" SelectedValuePath="Code" DisplayMemberPath="Descript"
SelectedItem="{Binding CbClientTypeSelectedItem}" MinWidth="70"></ComboBox>

對應的ViewModel代碼

        public CustomNodeItem _CbClientTypeSelectedItem;
        public CustomNodeItem CbClientTypeSelectedItem
        {
            get { return _CbClientTypeSelectedItem; }
            set
            {
                _CbClientTypeSelectedItem = value;
                //if (SelectCallBack != null)
                //{
                //    SelectCallBack(value);
                //}
                NotifyOfPropertyChange();
            }
        }

        private List<CustomNodeItem> _CbClientType;
        public List<CustomNodeItem> CbClientType
        {
            get { return _CbClientType; }
            set
            {
                _CbClientType = value;
                if (value != null && value.Count > 0 && CbClientTypeSelectedItem == null)
                {
                    CbClientTypeSelectedItem = value.First();
                }
                NotifyOfPropertyChange();
            }
        }

        //在按鈕的事件中獲取選擇項
        public void BtnTest()
        {
            string msg = CbClientTypeSelectedItem.Descript;
            MessageBox.Show(msg);
        }

 說明:這個應用在Caliburn.Micro中使用的


免責聲明!

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



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