2016-07-05 66 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}"/> 
相关问题