2014-07-22 128 views
5

我使用ItemsControl來保存我的集合。 的ItemsPanelCanvas,該ItemTemplateBorder>StackPanel>TextBlocks 塊我要綁定在DataTemplate一個命令捕獲在塊(我的收藏項目)的點擊在DataTemplate中的WPF命令綁定

代碼:

<Grid Grid.Row="1" Grid.Column="1" > 
     <ItemsControl ItemsSource="{Binding Products}"> 
      <ItemsControl.ItemsPanel> 
       <ItemsPanelTemplate> 
        <helpers:DragCanvas 
         HorizontalAlignment="Stretch" 
         VerticalAlignment="Stretch" 
         AllowDragging="True" 
         AllowDragOutOfView="False" /> 
       </ItemsPanelTemplate> 
      </ItemsControl.ItemsPanel> 
      <ItemsControl.ItemTemplate> 
       <DataTemplate> 
        <!-- The border and its content is what I see 
        on my canvas, I want to bind a command here (on click do something) --> 
        <Border BorderThickness="1" BorderBrush="Gold"> 
         <StackPanel Orientation="Horizontal"> 
          <TextBlock Text="{Binding Path=Name}" /> 
          <TextBlock Text="{Binding Path=Price}" /> 
         </StackPanel> 
        </Border> 
       </DataTemplate> 
      </ItemsControl.ItemTemplate>  
     </ItemsControl> 
    </Grid> 

回答

10

想要附加到命令的第一個對象是Border,因爲後者沒有Click事件,所以我將使用MouseLeftButtonDown,並且因爲命令僅用於機智^ h Button鹼基控件(按鈕,單選按鈕,複選框的RepeatButton ......),你也將需要EventTriggers,您的DataTemplate應該是這樣的:

<DataTemplate> 
     <Border BorderThickness="1" BorderBrush="Gold"> 
      <i:Interaction.Triggers> 
        <i:EventTrigger EventName="MouseLeftButtonDown"> 
          <command:EventToCommand Command="{Binding RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, Path=DataContext.MouseLeftButtonDown }"/> 
        </i:EventTrigger> 
      </i:Interaction.Triggers> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding Path=Name}" /> 
       <TextBlock Text="{Binding Path=Price}" /> 
      </StackPanel> 
     </Border> 
</DataTemplate> 

由於您的ItemsControl的來源必然產品,那麼,在DataTemplate中的DataContext的將是一個產品對象,以避免你應該命令的源綁定到包含RelayCommand該公司的DataContext綁定到視圖模型的Window祖先:

public class MainViewModel : ViewModelBase 
{ 


    public class Product 
    { 
     public string Name { get; set; } 
     public string Price { get; set; } 
    } 

    public List<Product> Products 
    { 
     get 
     { 
      return new List<Product>() 
        { 
         new Product(){Name = "Product1",Price = "Price1"}, 
         new Product(){Name = "Product2",Price = "Price2"} 
        }; 
     } 
    } 

    public RelayCommand MouseLeftButtonDown { get; set; } 

    public MainViewModel() 
    { 
      MouseLeftButtonDown = new RelayCommand(()=> MessageBox.Show("Message","Hi")); 
    } 
} 

PS:本command:EventToCommand是MVVM光強,如果你沒有使用MVVM-Light只是去用這個代替:

<i:Interaction.Triggers> 
     <i:EventTrigger EventName="MouseLeftButtonDown"> 
       <i:InvokeCommandAction Command="{Binding RelativeSource={RelativeSource AncestorType=Window, Mode=FindAncestor}, Path=DataContext.MouseLeftButtonDown }" >    
       </i:InvokeCommandAction> 
     </i:EventTrigger> 
</i:Interaction.Triggers> 

這應該完美的工作,希望我解釋得很好。

7

你可以試試這樣的事情:爲輸入

<DataTemplate> 
     <Border BorderThickness="1" BorderBrush="Gold"> 
     <Border.InputBindings> 
      <MouseBinding MouseAction="LeftClick" Command="{Binding DataContext.SomeCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ItemsControl}}}"/> 
     </Border.InputBindings> 

     <StackPanel Orientation="Horizontal"> 
      <TextBlock Text="{Binding Path=Name}" /> 
      <TextBlock Text="{Binding Path=Price}" /> 
      </StackPanel> 
      </Border> 
</DataTemplate> 
+0

+1結合, -1爲'命令= 「{結合SomeCommand}」',DataTemplate中的DataContext設置產品(型號)不夠ViewModel – AymenDaoudi

+1

不夠公平。編輯。 – Den