2012-10-26 95 views
2

我在用戶控件中有以下列表視圖。我想在整行上註冊雙擊。當雙擊註冊時,我想在我的視圖模型中調用ICommand雙擊列表視圖中的一行

有誰知道我能做到這一點?

我知道還有關於這個的其他帖子,我已經嘗試過在他們中找到的解決方案(使用MVVM Light和AttachedCommandBehavior),但無論是雙擊都沒有註冊,或者它試圖在我的TVSeries對象上調用ICommand是我的模型,它保存行中顯示的數據。

這是我的列表視圖:

<ListView Height="456" 
      HorizontalAlignment="Left" 
      Margin="12,12,0,0" 
      VerticalAlignment="Top" 
      Width="488" 
      ItemsSource="{Binding Path=Library}" 
      SelectedItem="{Binding Path=SelectedTVSeries}"> 

    <ListView.View> 

     <GridView> 

      <GridViewColumn Header="Name" 
          Width="Auto"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Path=Name}" 
            FontWeight="Bold" /> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 

      <GridViewColumn Header="Year" 
          Width="Auto"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Path=Year}" /> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 

      <GridViewColumn Header="Genre" 
          Width="Auto"> 
       <GridViewColumn.CellTemplate> 
        <DataTemplate> 
         <TextBlock Text="{Binding Path=Genre}" /> 
        </DataTemplate> 
       </GridViewColumn.CellTemplate> 
      </GridViewColumn> 

     </GridView> 

    </ListView.View> 

</ListView> 

UPDATE

當使用AttachedCommandBehavior我會收到以下錯誤:

NullReferenceException was unhandled

Object reference not set to an instance of an object.

該錯誤發生在下面的行(其AttachedCommandBehavior的一部分):

/// <summary> 
/// Executes the strategy 
/// </summary> 
public void Execute() 
{ 
    strategy.Execute(CommandParameter); 
} 

我使用ACB像的下方。首先,我爲ListViewItem聲明我的樣式。

< - 語言:XML - >

<UserControl.Resources> 
    <Style x:Key="LibraryListViewItemStyle" 
      TargetType="{x:Type ListViewItem}"> 
     <Setter Property="acb:CommandBehavior.Event" Value="MouseDoubleClick" /> 
     <Setter Property="acb:CommandBehavior.Action" Value="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=DataContext.TVSeriesDoubleClickedCommand}" /> 
     <Setter Property="acb:CommandBehavior.CommandParameter" Value="{Binding }" /> 
    </Style> 
</UserControl.Resources> 

然後,我在我的列表視圖樣式設置ItemContainerStyle

<ListView Height="456" 
      HorizontalAlignment="Left" 
      Margin="12,12,0,0" 
      VerticalAlignment="Top" 
      Width="488" 
      ItemsSource="{Binding Path=Library}" 
      SelectedItem="{Binding Path=SelectedTVSeries}" 
      ItemContainerStyle="{StaticResource LibraryListViewItemStyle}"> 

... 

</ListView> 

有誰知道如何解決這個問題?這真的是我最親密的。

**更新:解決方法出現**

上述錯誤,因爲我已經配置AttachedCommandBehavior預期的行動,但我已經實現了一個命令。從以下

<Setter Property="acb:CommandBehavior.Action" Value="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=DataContext.TVSeriesDoubleClickedCommand}" />

更改以下

<Setter Property="acb:CommandBehavior.Command" Value="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=DataContext.TVSeriesDoubleClickedAction}" />

這解決了這個錯誤。

然而,我發現,我更喜歡用一個動作,使我一直acb:CommandBehavior.Action和改變了我的實現:

public Action<object> TVSeriesDoubleClickedAction 
{ 
    get 
    { 
     return new Action<object>(tvSeries => Console.WriteLine("*** Double clicked fired: {0}", ((TVSeries)tvSeries).Name)); 
    } 
} 

回答

0

or it tries to call the ICommand on my TVSeries object which is my model and which holds the data presented in the rows.

這會發生,因爲你有你的綁定到你的ICommand的設置不正確。我建議要回你試過它被調用的ICommand的TVSeries對象的解決方案之一,並嘗試改變你的結合類似這樣:

Binding="{Binding RelativeSource={RelativeSource AncestorType=ListView}, Path=DataContext.YourCommandName}" 
+0

當使用'AttachedCommandBehaviour'目標是我模型,所以我嘗試了你的建議,而當我靠近時,它仍然不起作用。我現在會得到一個'NullReferenceException'。你可以看到我的原始問題了解更多信息。 – simonbs

+0

當處於NullReferenceException錯誤區域的中斷模式時,你能分辨哪個對象爲空?我的猜測是戰略對象?那是什麼,它應該在哪裏設置? –

+0

'strategy'爲null,'CommandParameter'爲我的模型('TVSeries'類的一個實例)的一個實例。 「策略」應該是「IExecutionStrategy」的一個實例。有兩個可能的地方可以設置,既可以在Command中使用,也可以在Action中使用setter,但這些都不會被調用。請注意,這不是我的代碼。這是AttachedCommandBehaviour圖書館的一部分。 – simonbs