我使用Prism for Windows Runtime來將View中的事件與ViewModels中的DelegateCommands連接起來。我想知道什麼是從包含按鈕(或自定義控件派生的freom Button類)的ListView調用命令(例如,選擇項目)的最佳方式。我想保留Button控件提供的效果(例如背景更改,傾斜效果)。但按鈕遺憾的是吸收了點擊事件,其中,在後果,我不能在ListView使用掛鉤,例如與下面的XAML(和行爲SDK)我的命令:在MVVM中調用ListView中的命令
<ListView ItemsSource="{Binding AvailableItemsList}" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<customControls:NavMenuButton Style="{StaticResource SelectionListMenuButton}" Content="{Binding Nickname}" DescriptionText="{Binding Name}" />
</DataTemplate>
</ListView.ItemTemplate>
<i:Interaction.Behaviors>
<core:EventTriggerBehavior EventName="SelectionChanged">
<core:InvokeCommandAction Command="{Binding ItemSelectedCommand}" />
</core:EventTriggerBehavior>
</i:Interaction.Behaviors>
</ListView>
什麼將是最好的方式做到這一點?我發現了類似的問題,但這裏的區別在於列表項中的控件顯然是「竊取」了點擊事件(儘管它適用於例如簡單的TextBlock)。
爲什麼不綁定的SelectedItem而不是使用eventrtrigger的東西? – blindmeis 2014-10-09 06:05:57
嗡嗡聲你想在你的視圖模式中從你的按鈕在你的視圖模式中調用methode?如果是這樣,你只需要使用listviewdatacontext並像這樣調用你的命令。 Command =「{Binding DataContext.YourCommand,elementName = yourListView} – MatDev8 2014-10-09 09:22:14
@blindmeis:你的意思是SelectionChanged?我的理解是它沒有實現ICommand,所以我需要在代碼後面添加一些邏輯。真的問題,但以某種方式打破了模式,我誤解了嗎? – jerry 2014-10-09 12:23:45