WPF双命令执行

时间:2014-09-01 08:11:06

标签: c# wpf xaml treeview setter

我有这段代码:

<TreeView Name="ExplorerTreeViewStandard" Grid.Row="2" Margin="0,10,10,10" VerticalAlignment="Stretch" ItemsSource="{Binding TreeNodes}" FontSize="14" Background="Transparent" BorderThickness="0"
                    dd:DragDrop.IsDragSource="True" dd:DragDrop.IsDropTarget="True" dd:DragDrop.DropHandler="{Binding}" dd:DragDrop.UseDefaultDragAdorner="True">
    <TreeView.ItemContainerStyle>
        <!-- This Style binds a TreeViewItem to a TreeViewNodeViewModel -->
        <Style TargetType="{x:Type TreeViewItem}">
            <!--<EventSetter Event="Selected" Handler="TreeViewSelectedItemChanged" />-->
            <!--<EventSetter Event="Expanded" Handler="TreeExpanded" />-->
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="FontWeight" Value="DemiBold" />
            <Setter Property="FontSize" Value="16" />
            <Setter Property="Foreground" Value="DarkBlue" />
            <Setter Property="mvvm:EventToCommand.Event" Value="MouseDoubleClick" />
            <Setter Property="mvvm:EventToCommand.Command" Value="{Binding RapidExecutionCommand}" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold" />
                    <Setter Property="Foreground" Value="Yellow" />
                    <Setter Property="Background" Value="Blue" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>

    <TreeView.Resources>
        <!--/////////////////////////////////////-->
        <!-- Provide the Templates per Node Type -->
        <!--/////////////////////////////////////-->
        <!-- Root Node Template -->
        <HierarchicalDataTemplate DataType="{x:Type local:TreeViewRootNodeViewModel}" ItemsSource="{Binding Children}" >
            <StackPanel Orientation="Horizontal">
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Refresh" Command="{Binding RefreshCommand}" />
                        <Separator />
                        <MenuItem Header="New Folder" Command="{Binding NewNodeCommand}" CommandParameter="{x:Static bo:NodeTypes.F}" />
                        <MenuItem Header="New Project" Command="{Binding NewNodeCommand}" CommandParameter="{x:Static bo:NodeTypes.P}" />
                        <Separator />
                        <MenuItem Header="Paste" Visibility="{Binding Path=CanPaste, Converter={StaticResource BoolToVisibilityConverter}}" Command="{Binding CutCopyPasteNodeCommand}" CommandParameter="{x:Static local:NodeOperations.Paste}" />
                    </ContextMenu>
                </StackPanel.ContextMenu>
                <Image Width="16" Height="16" Source="{Binding NodeIcon}" VerticalAlignment="Center"  />
                <TextBlock Text="{Binding NodeData}" VerticalAlignment="Center" Margin="3,0,0,0" FontWeight="Bold" FontStyle="Italic" FontSize="20" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <!-- Folder Node Template -->
        <HierarchicalDataTemplate DataType="{x:Type local:TreeViewFolderNodeViewModel}" ItemsSource="{Binding Children}" >
            <StackPanel Orientation="Horizontal">
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Refresh" Command="{Binding RefreshCommand}" />
                        <Separator />
                        <MenuItem Header="New Folder" Command="{Binding NewNodeCommand}" CommandParameter="{x:Static bo:NodeTypes.F}" />
                        <MenuItem Header="New Project" Command="{Binding NewNodeCommand}" CommandParameter="{x:Static bo:NodeTypes.P}" />
                        <Separator />
                        <MenuItem Header="Cut" Command="{Binding CutCopyPasteNodeCommand}" CommandParameter="{x:Static local:NodeOperations.Cut}" />
                        <!--Copy folder disabled per bug# 1617 Dec-27-2013-->
                        <!--<MenuItem Header="Copy" Command="{Binding CutCopyPasteNodeCommand}" CommandParameter="{x:Static local:NodeOperations.Copy}" />-->
                        <MenuItem Header="Paste" Visibility="{Binding Path=CanPaste, Converter={StaticResource BoolToVisibilityConverter}}" Command="{Binding CutCopyPasteNodeCommand}" CommandParameter="{x:Static local:NodeOperations.Paste}" />
                        <Separator />
                        <MenuItem Header="Delete" Command="{Binding DeleteFolderCommand}" />
                    </ContextMenu>
                </StackPanel.ContextMenu>
                <Image Width="16" Height="16" Source="{Binding NodeIcon}" VerticalAlignment="Center"  />
                <TextBlock Text="{Binding NodeData}" VerticalAlignment="Center" Margin="3,0,0,0" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <!-- Project Node Template -->
        <HierarchicalDataTemplate DataType="{x:Type local:TreeViewProjectNodeViewModel}" ItemsSource="{Binding Children}" >
            <StackPanel Orientation="Horizontal">

                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Refresh" Command="{Binding RefreshCommand}" />
                        <Separator />
                        <MenuItem Header="Rapid Execution" Command="{Binding RapidExecutionCommand}" Visibility="{Binding Path=EnableRapidExecution, UpdateSourceTrigger=PropertyChanged}" />
                        <Separator />
                        <!--Bug 1700-->
                        <!--<MenuItem Header="New Folder" Command="{Binding NewNodeCommand}" CommandParameter="{x:Static bo:NodeTypes.F}" />
                        <MenuItem Header="New Project" Command="{Binding NewNodeCommand}" CommandParameter="{x:Static bo:NodeTypes.P}" />-->
                        <!--<Separator />-->
                        <MenuItem Header="Cut" Command="{Binding CutCopyPasteNodeCommand}" CommandParameter="{x:Static local:NodeOperations.Cut}" />
                        <!--Copy project disabled per bug# 1617 Dec-27-2013-->
                        <!--<MenuItem Header="Copy" Command="{Binding CutCopyPasteNodeCommand}" CommandParameter="{x:Static local:NodeOperations.Copy}" />-->
                        <MenuItem Header="Paste" Visibility="{Binding Path=CanPaste, Converter={StaticResource BoolToVisibilityConverter}}" Command="{Binding CutCopyPasteNodeCommand}" CommandParameter="{x:Static local:NodeOperations.Paste}" />
                        <Separator />
                        <MenuItem Header="Disable Project" Command="{Binding DisableProjectCommand}" Visibility="{Binding Path=EnableDisable, UpdateSourceTrigger=PropertyChanged}" />
                        <MenuItem Header="Resume Project" Command="{Binding ResumeProjectCommand}" Visibility="{Binding Path=EnableResume, UpdateSourceTrigger=PropertyChanged}" />
                        <MenuItem Header="Clone Project" Command="{Binding CloneProjectCommand}" Visibility="{Binding Path=EnableClone, UpdateSourceTrigger=PropertyChanged}" />
                        <MenuItem Header="Clone Related Project" Command="{Binding CloneRelatedProjectCommand}"  />
                        <MenuItem Header="Update Group Properties" Visibility="{Binding Path=EnableRelated, UpdateSourceTrigger=PropertyChanged}" >
                            <MenuItem Header="Split Project"  Command="{Binding SplitRelatedProjectCommand }"  />
                            <MenuItem Header="All Related Projects" Command="{Binding UpdateRelatedProjectCommand }" />
                            </MenuItem>


                        <Separator />
                        <MenuItem Header="Show Versions" Command="{Binding ShowVersionsCommand}" />
                        <MenuItem Header="Collapse Versions" Command="{Binding CollapseVersionsCommand}" />
                        <!--<Separator />-->
                        <!--<MenuItem Header="Delete" Command="{Binding DeleteNodeCommand}" />-->
                    </ContextMenu>
                </StackPanel.ContextMenu>
                <Image Width="16" Height="16" Source="{Binding NodeIcon}" VerticalAlignment="Center"  />
                <TextBlock Text="{Binding NodeData}" VerticalAlignment="Center" Margin="3,0,0,0" />
            </StackPanel>
        </HierarchicalDataTemplate>
        <!-- Version Node Template -->
        <HierarchicalDataTemplate DataType="{x:Type local:TreeViewVersionNodeViewModel}" ItemsSource="{Binding Children}" >
            <StackPanel Orientation="Horizontal">
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="Rapid Execution" Command="{Binding RapidExecutionVersionCommand}" Visibility="{Binding Path=EnableRapidExecution, UpdateSourceTrigger=PropertyChanged}" />
                        <Separator />   
                        <MenuItem Header="ReOpen" Command="{Binding ReOpenCommand}" />
                    </ContextMenu>
                </StackPanel.ContextMenu>
                <Image Width="16" Height="16" Source="{Binding NodeIcon}" VerticalAlignment="Center"  />
                <TextBlock Text="{Binding NodeData}" VerticalAlignment="Center" Margin="3,0,0,0" />
            </StackPanel>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

问题是,当我双击并运行“RapidExecutionCommand”时,我在版本类和项目类中拥有它(项目是版本的父亲)。 所以当我双击项目时一切都很好,但是当我双击版本时,它会从版本和项目中运行命令。

任何sujestions如何解决这个问题?

我试着这个:

 <Setter Property="mvvm:EventToCommand.Event" Value="MouseDoubleProjectClick" />
 <Setter Property="mvvm:EventToCommand.Command" Value="{Binding MouseDoubleProjectClick}" />
 <Setter Property="mvvm:EventToCommand.Event" Value="MouseDoubleVersionClick" />
 <Setter Property="mvvm:EventToCommand.Command" Value="{Binding MouseDoubleVersionClick}" />

但是没有工作,只有项目被淘汰。

新编辑: 添加此项,但e.handler始终为false。

private static void CommandChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
    Control control = target as Control;
    if (control != null)
    {

        if ((e.NewValue != null) && (e.OldValue == null))
        {
            control.MouseDoubleClick += OnMouseDoubleClick;
        }
        else if ((e.NewValue == null) && (e.OldValue != null))
        {
            control.MouseDoubleClick -= OnMouseDoubleClick;
        }
    }
}

private static void OnMouseDoubleClick(object sender, RoutedEventArgs e)
{
    Control control = sender as Control;
    ICommand command = (ICommand)control.GetValue(CommandProperty);
    object commandParameter = control.GetValue(CommandParameterProperty);
    command.Execute(commandParameter);
    e.Handled = true;
}

任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

您需要将事件标记为已处理,以便它不会在WPF VisualTree上进一步传播。

电话会议类似于:

e.Handled = true;

其中e是您的事件处理程序的EventArgs参数。