MVVM:給dropdown註冊事件

來源:互聯網
上載者:User

1.xaml檔案
 <ComboBox x:Name="cmbDepartmentID" Grid.Row="1"  Width="200"
                      Margin="4" Height="25"
                      ItemsSource="{Binding Path=PersonList}"
                      DisplayMemberPath="Name"
                      SelectedValuePath="Name"
                      SelectedValue="{Binding Path=CurrentItem, Mode=TwoWay}">
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="SelectionChanged">
                        <i:InvokeCommandAction Command="{Binding CommandQuery}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
            </ComboBox>
2.資料來源PersonList,當前選中的CurrentItem

 ObservableCollection<Person> personList;
 public ObservableCollection<Person> PersonList
        {
            get { return personList; }
            set { personList = value; }
        }
建構函式裡面添加資料來源

public MainPageViewModel()
        {
            personList = new ObservableCollection<Person>();
            personList.Add(new Person() { Id = 1, Name = "張三", Address = "東京" });
            personList.Add(new Person() { Id = 2, Name = "李四", Address = "洛陽" });
            personList.Add(new Person() { Id = 3, Name = "王五", Address = "神都" });
            personList.Add(new Person() { Id = 4, Name = "趙六", Address = "帝都" });
            personList.Add(new Person() { Id = 5, Name = "錢七", Address = "魔都" });
        }

當前選中的CurrentItem
 string currentItem;
 public string CurrentItem
        {
            get { return currentItem; }
            set
            {
                if (!ReferenceEquals(currentItem, value))
                {
                    currentItem = value;
                    this.RaisePropertyChanged(() => this.CurrentItem);
                }
            }
        }

3.添加事件
 private ICommand commandQuery;
        public ICommand CommandQuery
        {
            get { return commandQuery ?? (commandQuery = new DelegateCommand(this.MethodQuery)); }
        }
4.當選中其中一個的時候,進行的操作

private void MethodQuery()
        {
            //當選中其中一個的時候,進行的操作           
             MessageBox.Show(CurrentItem);
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.