DataTemplate中的WPF命令绑定

Sta*_*ked 6 data-binding wpf datatemplate mvvm

我正在使用ItemsControl我的藏品.的ItemsPanelCanvas,该ItemTemplate是块Border> StackPanel> TextBlocks 我要绑定在一个命令DataTemplate捕获在块上单击(我的收藏项目)

码:

   <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>
Run Code Online (Sandbox Code Playgroud)

Aym*_*udi 12

想要附加到命令的第一个对象是Border,因为后者没有Click事件,我将使用MouseLeftButtonDown,因为命令仅用于Button-base控件(Button,RadioButton,CheckBox,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>
Run Code Online (Sandbox Code Playgroud)

由于您的ItemsControl的源绑定到Products,因此DataTemplate的DataContext将是一个Product对象,以避免您将命令的源绑定到Window祖先,其DataContext绑定到包含RelayCommand的ViewModel:

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"));
    }
}
Run Code Online (Sandbox Code Playgroud)

PS:command:EventToCommand来自MVVM-Light,如果你不使用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>
Run Code Online (Sandbox Code Playgroud)

这应该是完美的,希望我解释得很好.


Den*_*Den 9

你可以尝试这样的事情:

<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>
Run Code Online (Sandbox Code Playgroud)