2017-08-29 71 views
0

內點擊了項這是一個佈局我在XAML:EventToCommand得到一個ItemsControl

<ScrollViewer Name="svDesigner" HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Focusable="False"> 
    <ItemsControl ItemsSource="{Binding DesignerRowsCollection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> 
     <ItemsControl.ItemTemplate> 
      <DataTemplate DataType="{x:Type base:UI_DesignerRow}"> 
       <cntrl:BabDesignerRow Margin="2"/> 
      </DataTemplate> 
     </ItemsControl.ItemTemplate> 
     <dxmvvm:Interaction.Behaviors> 
      <dxmvvm:EventToCommand EventName="MouseLeftButtonDown" 
           Command="{Binding SetPropertiesSourceCommand}" PassEventArgsToCommand="True"> 
       <dxmvvm:EventToCommand.EventArgsConverter> 
        <conv:DesignerItemClickEventArgsConverter/> 
       </dxmvvm:EventToCommand.EventArgsConverter> 
      </dxmvvm:EventToCommand> 
     </dxmvvm:Interaction.Behaviors> 
    </ItemsControl> 

在後面的代碼我有這樣子它處理的ScrollViewer的的PreviewMouseLeftButtonDown事件:

Private Sub DesignerItemSelectionRedirect(sender As Object, e As MouseButtonEventArgs) Handles svDesigner.PreviewMouseLeftButtonDown 

在我的UserControl內部BabDesignerRow我有很多像TextBox,ComboBox,LayoutGroup等不同的UI對象。

現在,無論我點擊什麼,我總是將整個ItemsControl作爲e.Source獲取,而不是選定的真實對象。

有沒有辦法讓我的UserControl內點擊對象?

我需要獲取此信息以突出顯示用戶選擇的內容。

回答

1

這是因爲路由事件的隧道策略。您應該詳細瞭解路由事件及其冒泡和隧道策略。

您可以使用此事件的冒泡版本:MouseLeftButtonDown。作爲MSDN states

儘管這似乎是通過一個元素 樹跟隨冒泡的路線,它實際上是提出並沿着各自的UIElement元素樹重新拋出 直接路由事件。

所以這不是一個真正的冒泡事件,但它應該適用於你的情況。

或者,您可以使用e.OriginalSource屬性而不是e.Source來獲取導致此事件的元素。