2017-10-23 6 views
1

したがって、基本的に私はクラスステップを持っています。DataTemplateを使用してItemsControl内のオブジェクトを渡す

public class Step : INotifyPropertyChanged 
{ 
    public int Index { get; set; } 
    public int Time { get; set; } 
} 

私のxamlでは、1つのDataTemplateとItemsControlを使用してステップを表現したいと考えています。私のDataTemplateはこのようになります:

<DataTemplate x:Key="StepTemplate" DataType="data:Step"> 
    <Label Content="{Binding Index}"/> 
    <Label Content="{Binding Time}"/> 
</DataTemplate> 

そして、私のItemsControlはこのようになります。

<ItemsControl Name="ListSteps" ItemsSource="{Binding Steps}" Grid.IsSharedSizeScope="True" ItemTemplate="{StaticResource StepTemplate}" > 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="MouseLeftButtonDown"> 
      <i:InvokeCommandAction Command="{Binding Path=DataContext.StepClick, ElementName=ListSteps}" CommandParameter="{Binding}"/> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</ItemsControl> 

Soはほとんどどうするか:私はStepをクリックするたびに、私はクリック呼び出さStepClick方法とステップオブジェクトがパラメータとして渡されます。

実際に起こっている何

:私は、Stepに呼び出されるStepClick方法をクリックしますが、ステップのオブジェクトがパラメータとして渡されますが、私の見解のオブジェクトは渡さないときはいつでも。それは私が欲しいものではありません。

Stepをクリックすると、どのようにオブジェクトをStepに渡すことができますか?

答えて

1

ItemTemplateのルート要素に相互作用トリガーを移動:

<DataTemplate x:Key="StepTemplate" DataType="data:Step"> 
    <StackPanel Background="Transparent"> 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="MouseLeftButtonDown"> 
       <i:InvokeCommandAction Command="{Binding Path=DataContext.StepClick, RelativeSource={RelativeSource AncestorType=ItemsControl}}" 
             CommandParameter="{Binding}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
     <Label Content="{Binding Index}"/> 
     <Label Content="{Binding Time}"/> 
    </StackPanel> 
</DataTemplate> 
関連する問題