列表框选择隐藏其他鼠标事件

本文关键字:鼠标 事件 其他 隐藏 选择 列表 | 更新日期: 2023-09-27 17:55:29

Hello Stackoverflowers,

我有一个System.Windows.Control.ListBox。它做得很好,但我希望在选择某些类型的项目时有一些行为。

我无法在 SelectedItem 的绑定属性中执行此操作,因为我的列表框的视图模型 (Foo) 不知道我想要的工作所需的所有数据(一些来自另一个 ViewModel:Bar)。

我提到的两个ViewModel是一个更大的Zot类的领域,以便Zot访问Foo和Bar的内容。

我使用 Interaction.Triggers、EventTrigger 和 InvokeCommandAction 将 Foo 和 Bar 中的点击事件转发到 Zot。它非常适合酒吧(这是一块画布)。但是我在使用列表框时遇到问题。

在测试事件选择更改、鼠标按下和单击后,如果我单击包装列表框的网格,则似乎会触发鼠标按下,但当我单击列表框时不会触发。感觉列表框中的嵌入选择与其他事件冲突。

任何人都有任何想法根据所选项目在不同的视图模型中执行特定操作?

多谢

编辑:

下面是列表框的 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 个类似的事件完美运行(在画布上)。我尝试使用鼠标向下而不是选择更改,当我单击包含列表框的网格时会触发它,但当我单击列表框时不会触发。

(在 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 事件以执行特殊工作。

我可以在绑定到列表框的 ItemSelect 的属性的 setter 中执行此操作,但要做的工作需要列表框视图模型中未知的数据,这就是为什么我有一个主窗口视图模型来保存列表框的视图模型,我尝试在主窗口视图模型(和另一个视图模型)中获取 SelectionChanged 事件。

如果不清楚,请告诉我。

列表框选择隐藏其他鼠标事件

您正在尝试在工具箱视图中设置一个不知道任何 SelectionChanged 事件的 SelectionChanged 事件。

您可以在工具箱视图中创建两个 DP 来存储命令及其参数:

#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}"/>