2016-07-05 64 views
0

你好Stackoverflowers,列表框選擇隱藏其他鼠標事件

我有一個System.Windows.Control.ListBox。它做得很好,但我想在選擇某些類型的項目時有一些行爲。

我不能在SelectedItem的bind屬性中做它,因爲我的Listbox的視圖模型(Foo)不知道我想要的工作(某些來自另一個ViewModel:Bar)的所有需要​​的數據。

我的兩個提到視圖模型是一個更大的類ZOT的領域,爲了ZOT來訪問Foo和Bar

的內容

我盼着點擊Foo和Bar事件中使用Interaction.Triggers到ZOT,EventTrigger和InvokeCommandAction。它對Bar(這是一個畫布)非常有用。不過,我在列表框中遇到了問題。

在測試事件SelectionChanged,MouseDown和Click之後,如果我單擊包裝列表框的網格,但不是當我單擊列表框時,似乎會觸發MouseDown。感覺就像列表框中的嵌入式選擇與其他事件相沖突。

任何人都有任何想法做不同的viewmodel取決於選定的項目的具體行動?

非常感謝

編輯:

這裏是XAML的列表框(在ToolboxView.xaml)

 d:DataContext="{d:DesignInstance viewModels:ToolboxViewModel}"> 
     <Grid> 
      <ListBox 
       ItemsSource="{Binding Tools}" 
       SelectedItem="{Binding SelectedTool}" 
       x:Name="ToolView" > 

      <ListBox.ItemTemplate> 
       <DataTemplate DataType="interfaces:IBuilder"> 
        <TextBlock 
         FontWeight="DemiBold" 
         HorizontalAlignment="Center" 
         VerticalAlignment="Center" 
         Text="{Binding Name}"/> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 
    </Grid> 

這裏是關於列表框的情況下,從主窗口xaml(哪個視圖模型保存列表框視圖模型,我解釋下面的原因)。然而事件從未被觸發。後來在同一個文件中,3個類似的事件完美地工作(在畫布上)。我試圖使用MouseDown而不是SelectionChanged,它會在包含列表框的網格中單擊時觸發,但當單擊列表框時不會觸發。

(在MainWindow.xaml)

<DockPanel> 
     <views:ToolboxView DockPanel.Dock="Left" 
           Width="120" 
           IsHitTestVisible="True" 
           DataContext="{Binding ToolBoxViewModel}" 
           x:Name="ToolboxView"> 

      <i:Interaction.Triggers> 
       <i:EventTrigger EventName="SelectionChanged"> 
        <i:InvokeCommandAction Command="{Binding Path=DataContext.SelectionChangedCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
              CommandParameter="{Binding ElementName=ToolboxOverlayView}"/> 
       </i:EventTrigger> 
      </i:Interaction.Triggers> 

現在我所謂的「嵌入式選擇」是列表框,我可以突出列表框裏面的元素,選擇它的行爲。這與上面的代碼完美的結合(我可以選擇我的工具,在ViewModel中綁定的屬性相應地改變)。我想要做的是在選擇列表框中的某個類別的元素時觸發SelectionChanged事件來做特殊工作。

我可以在綁定到Listbox的ItemSelected屬性的setter中做到這一點,但需要做的工作需要列表框視圖模型中未知的數據,這就是爲什麼我有一個mainwindow視圖模型來保存listbox的視圖模型,我嘗試在主窗口視圖模型(和另一個視圖模型)中獲取SelectionChanged事件。

請告訴我,如果它不清楚請。

+1

我們需要見你xaml。例如,這可能是空的背景,但我們不知道,因爲您沒有向我們展示您的代碼 – nkoniishvt

+0

請告訴我們您實際上想要做的具體措施,並向我們展示代碼。另外,我想要做的最後一件事是使你的感受無效,但我不確定什麼是「嵌入式選擇」,或者它爲什麼會給你那些特殊的感覺。 –

+0

@nkoniishvt我編輯給你更多信息 – Csi

回答

1

您正試圖在ToolboxView中設置SelectionChanged事件,該事件不知道任何SelectionChanged事件。

您可以創建兩個DP在ToolboxView存儲的命令及其參數:

#region SelectionChangedCommand 

public ICommand SelectionChangedCommand 
{ 
    get { return (ICommand)GetValue(SelectionChangedCommandProperty); } 
    set { SetValue(SelectionChangedCommandProperty, value); } 
} 

private readonly static FrameworkPropertyMetadata SelectionChangedCommandMetadata = new FrameworkPropertyMetadata { 
    DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
}; 

public static readonly DependencyProperty SelectionChangedCommandProperty = 
    DependencyProperty.Register("SelectionChangedCommand", typeof(ICommand), typeof(ToolboxView), SelectionChangedCommandMetadata); 
#endregion 


#region SelectionChangedCommandParameter 

public Object SelectionChangedCommandParameter 
{ 
    get { return (Object)GetValue(SelectionChangedCommandParameterProperty); } 
    set { SetValue(SelectionChangedCommandParameterProperty, value); } 
} 

private readonly static FrameworkPropertyMetadata SelectionChangedCommandParameterMetadata = new FrameworkPropertyMetadata { 
    DefaultUpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
}; 

public static readonly DependencyProperty SelectionChangedCommandParameterProperty = 
    DependencyProperty.Register("SelectionChangedCommandParameter", typeof(Object), typeof(ToolboxView), SelectionChangedCommandParameterMetadata); 
#endregion 

然後在ToolboxView.xaml:

<Grid> 
    <ListBox 
     ItemsSource="{Binding Tools}" 
     SelectedItem="{Binding SelectedTool}" 
     x:Name="ToolView" > 
     <i:Interaction.Triggers> 
      <i:EventTrigger EventName="SelectionChanged"> 
       <i:InvokeCommandAction Command="{Binding Path=SelectionChangedCommand, RelativeSource={RelativeSource AncestorType={x:Type ToolboxView}}}" 
             CommandParameter="{Binding Path=SelectionChangedCommandParameter, RelativeSource={RelativeSource AncestorType={x:Type ToolboxView}}}"/> 
      </i:EventTrigger> 
     </i:Interaction.Triggers> 
     <ListBox.ItemTemplate> 
      <DataTemplate DataType="interfaces:IBuilder"> 
       <TextBlock 
        FontWeight="DemiBold" 
        HorizontalAlignment="Center" 
        VerticalAlignment="Center" 
        Text="{Binding Name}"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</Grid> 

而且在MainWindow.xaml使用它:

<views:ToolboxView DockPanel.Dock="Left" 
        Width="120" 
        IsHitTestVisible="True" 
        DataContext="{Binding ToolBoxViewModel}" 
        x:Name="ToolboxView" 
        SelectionChangedCommand="{Binding Path=DataContext.SelectionChangedCommand, RelativeSource={RelativeSource AncestorType={x:Type Window}}}" 
        SelectionChangedCommandParameter="{Binding ElementName=ToolboxOverlayView}"/> 
相關問題