第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

WPF將UI事件綁定到ViewModel中的命令

WPF將UI事件綁定到ViewModel中的命令

C#
瀟瀟雨雨 2019-08-12 18:05:26
WPF將UI事件綁定到ViewModel中的命令我正在做一些簡(jiǎn)單的應(yīng)用程序重構(gòu)以跟隨MVVM,我的問(wèn)題是如何將SelectionChanged事件從我的代碼中移出到viewModel?我已經(jīng)看了一些綁定元素到命令的例子,但是并沒(méi)有完全掌握它。誰(shuí)能幫忙解決這個(gè)問(wèn)題。謝謝!有人可以使用下面的代碼提供解決方案嗎?非常感謝!public partial class MyAppView : Window {     public MyAppView()     {         InitializeComponent();         this.DataContext = new MyAppViewModel ();         // Insert code required on object creation below this point.     }     private void contactsList_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)     {         //TODO: Add event handler implementation here.                    //for each selected contact get the labels and put in collection          ObservableCollection<AggregatedLabelModel> contactListLabels = new ObservableCollection<AggregatedLabelModel>();         foreach (ContactListModel contactList in contactsList.SelectedItems)         {             foreach (AggregatedLabelModel aggLabel in contactList.AggLabels)             {                 contactListLabels.Add(aggLabel);             }         }         //aggregate the contactListLabels by name         ListCollectionView selectedLabelsView = new ListCollectionView(contactListLabels);         selectedLabelsView.GroupDescriptions.Add(new PropertyGroupDescription("Name"));         tagsList.ItemsSource = selectedLabelsView.Groups;     }}
查看完整描述

3 回答

?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊

您應(yīng)該EventTriggerInvokeCommandActionWindows.Interactivity命名空間結(jié)合使用。這是一個(gè)例子:

<ListBox ...>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="SelectionChanged">
            <i:InvokeCommandAction Command="{Binding SelectedItemChangedCommand}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers></ListBox>

你可以System.Windows.Interactivity去參考Add reference > Assemblies > Extensions。

完整的i命名空間是:xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"。


查看完整回答
反對(duì) 回復(fù) 2019-08-12
?
慕村225694

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊

我處理這個(gè)問(wèn)題的方法是在ViewModel中有一個(gè)SelectedItem屬性,然后將ListBox的SelectedItem綁定到該屬性。


查看完整回答
反對(duì) 回復(fù) 2019-08-12
?
莫回?zé)o

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊

要重構(gòu)這個(gè),你需要改變你的想法。您將不再處理“選擇已更改”事件,而是將所選項(xiàng)目存儲(chǔ)在viewmodel中。然后,您將使用雙向數(shù)據(jù)綁定,以便在用戶選擇項(xiàng)目時(shí)更新您的viewmodel,并在更改所選項(xiàng)目時(shí)更新您的視圖。


查看完整回答
反對(duì) 回復(fù) 2019-08-12
  • 3 回答
  • 0 關(guān)注
  • 1777 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)