我需要綁定文本塊(或潛在的圖像以及 - 無論哪種方式,其用戶控件)的雙擊事件,我的ViewModel中的命令。如何將WPF中的命令綁定到控件的雙擊事件處理程序?
TextBlock.InputBindings似乎不正確綁定到我的命令,任何幫助嗎?
我需要綁定文本塊(或潛在的圖像以及 - 無論哪種方式,其用戶控件)的雙擊事件,我的ViewModel中的命令。如何將WPF中的命令綁定到控件的雙擊事件處理程序?
TextBlock.InputBindings似乎不正確綁定到我的命令,任何幫助嗎?
嘗試馬龍格列奇的attached command behaviors。
我也有地方,我需要到ListView的MouseDoubleClick事件綁定到我的視圖模型的命令類似的問題。
我想出了最簡單的辦法是把一個虛擬按鈕,有需要的命令綁定和調用MouseDoubleClick事件的事件處理程序的執行按鈕的命令的方法。
的.xaml
<Button Visibility="Collapsed" Name="doubleClickButton" Command="{Binding Path=CommandShowCompanyCards}"></Button>
<ListView MouseDoubleClick="ListView_MouseDoubleClick" SelectedItem="{Binding Path=SelectedCompany, UpdateSourceTrigger=PropertyChanged}" BorderThickness="0" Margin="0,10,0,0" ItemsSource="{Binding Path=CompanyList, UpdateSourceTrigger=PropertyChanged}" Grid.Row="1" HorizontalContentAlignment="Stretch" >
代碼隱藏
private void ListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
doubleClickButton.Command.Execute(null);
}
這並不簡單,但它是非常簡單的,它的作品。
<Button>
<Button.InputBindings>
<MouseBinding Gesture="LeftDoubleClick" Command="YourCommand" />
</Button.InputBindings>
</Button>
http://thejoyofcode.com/Invoking_a_Command_on_a_Double_Click_or_other_Mouse_Gesture.aspx
+1我個人認爲這個答案應該是標記爲正確的答案。它不僅是迄今爲止最簡單的,但它也可以通過在一個視圖模型結合到'ICommand'與MVVM使用:'
不工作(也許只有一些物品),直到你做出這樣的:http://www.telerik.com/community/forums/wpf/gridview/bind-double-click-to-a-viewmodel-command.aspx – 2011-12-06 08:33:52
不錯的選擇,凌亂的附加行爲:) – 2012-03-13 19:32:57
很簡單,讓我們使用MVVM方式: 我在這裏使用MVVM光這是很容易學習和堅強。
1.put以下行xmlns聲明:
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
xmlns:GalaSoft_MvvmLight_Command="clr-namespace:GalaSoft.MvvmLight.Command;
assembly=GalaSoft.MvvmLight.Extras.WPF4"
2.define你的文本塊就像這樣:
<textBlock text="Text with event">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<GalaSoft_MvvmLight_Command:EventToCommand
Command="{Binding Edit_Command}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</textBlock>
3.then寫你的命令代碼在您的視圖模型!
ViewModel1.cs
Public RelayCommand Edit_Command
{
get;
private set;
}
Public ViewModel1()
{
Edit_Command=new RelayCommand(()=>execute_me());
}
public void execute_me()
{
//write your code here
}
我希望對你的作品,因爲我已經在真正的ERP應用中使用它
這些'gala-soft-stuff'有點雜亂,但Interaction-approach很好! – 2015-12-09 18:23:40
我已經在我的工程三輔助類。我只希望WPF完全支持開發人員想要處理的所有這些事情,但我想這會及時到來。謝謝,這工作:) – bluebit 2009-08-18 13:32:03
我同意你的一般情緒 - 這是一個有點令人沮喪的支持MVVM沒有更多的烘烤到WPF。大多數經驗豐富的WPF開發人員已經建立了自己的小型輔助助手庫。如果你在做Silverlight,功能上的差距會更大! – 2009-08-18 13:36:40