2010-01-25 143 views
3

我試圖在用戶單擊ListView中的某個項目時執行ViewModel中的命令。當我在XAML中添加ListViewItem時,我只需將MouseBinding添加到其InputBindings即可。將MouseBindings添加到數據綁定中的項目WPF ListView

<ListView> 
<ListView.View> 
    <GridView> 
     <GridViewColumn Header="Test" /> 
    </GridView> 
    </ListView.View> 
    <ListViewItem Content="Item 1" > 
     <ListViewItem.InputBindings> 
     <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}" /> 
     </ListViewItem.InputBindings> 
</ListViewItem> 
</ListView> 

但是,如何才能在數據綁定ListView中實現?

<ListView ItemsSource="{Binding Patients}"> 
<ListView.View> 
    <GridView> 
     <GridViewColumn Header="Test" /> 
    </GridView> 
    <!-- How to set the MouseBinding for the generated ListViewItems?? --> 
</ListView.View> 

我已經通過定義ListViewItem風格和更換ListViewItemControlTempalte得到了解決。雖然,我希望有一個更簡單的解決方案。

真誠, 邁克爾

+0

你看過這篇文章嗎? http://stackoverflow.com/questions/1035023/firing-a-double-click-event-from-a-wpf-listview-item-using-mvvm – Brent 2010-01-25 16:17:13

+0

看看這篇文章在social.msdn:http:// social .msdn.microsoft.com/forums/zh-CN/wpf/thread/fb85577c-3704-492e-900c-1f0bffd1e4c2/ – 2010-01-25 18:33:36

回答

4

使用樣式更換上ListViewItemControlTemplate是個不錯的解決方案。事實上,這可能是我的第一選擇。

實現同種的另一種方法是使用你的ListViewItem風格的自定義附加屬性:

<Style TargetType="ListViewItem"> 
    <Setter Property="local:AddToInputBinding.Binding"> 
    <Setter.Value> 
     <MouseBinding Gesture="LeftDoubleClick" Command="{Binding DoubleClickCommand}" />  
    </Setter.Value> 
    </Setter> 
    ... 

要做到這一點,你需要創建MyBindingHandler.AddBinding附加屬性:

public class AddToInputBinding 
{ 
    public static InputBinding GetBinding(... // create using propa snippet 
    public static void SetBinding(... 
    public static readonly DependencyProperty BindingProperty = DependencyProperty.RegisterAttached(
    "Binding", typeof(InputBinding), typeof(AddToInputBinding), new PropertyMetadata 
    { 
    PropertyChangedCallback = (obj, e) => 
    { 
     ((UIElement)obj).InputBindings.Add((InputBinding)e.NewValue); 
    } 
    })); 
} 

這可以擴展爲處理多個綁定,但您明白了:此類允許您在任何樣式中添加InputBinding。

由於DoubleClick綁定是直接在ListBoxItem上定義的,而不是在其模板中的另一個控件上定義的,所以此解決方案可能比您所做的更爲可取。但我認爲這主要歸結爲個人偏好。

+0

我嘗試了您的解決方案,但無法使其正常工作。首先,我認爲GetBinding()和SetBinding()應該是靜態的(如果它們不是,則不編譯),其次,我總是得到錯誤「無法將屬性'Property'中的值轉換爲'System .Windows.DependencyProperty」。」當我嘗試從風格內使用它。它適用於我不把它放在風格里面。 – Teodor 2010-03-15 21:06:47

+0

我想也許你沒有使用propa片段。如果你有,它會爲你提供'靜態'。另外,propa應該通過正確設置'DependencyProperty'來解決你的其他問題。你是否設置了「RegisterAttached」的第三個參數? – 2010-03-16 09:18:13

+0

在WPF 3中。5 MouseBinding的Command屬性不是DependencyProperty,所​​以這段代碼將不起作用。這在我使用atm的WPF 4.0中得到了修復。 請參閱:http://blogs.msdn.com/llobo/archive/2009/10/29/new-wpf-features-key-gesture-binding.aspx – 2010-03-16 09:56:35

1

我能夠做解決此如下:

1)我添加了一個參考System.Windows.Interactivity DLL(發現它在C:\Program Files (x86)\Microsoft SDKs\Expression\Blend\.NETFramework\v4.0\Libraries

2)添加到了我的XAML文件:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity" 

3)增加本我的ListView裏面:

<ListView ...> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="MouseDoubleClick"> 
      <i:InvokeCommandAction Command="{x:Static local:MainWindow.RoutedCommandEditSelectedRecordWindow}" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
    ... 

</ListView> 
相關問題