2013-07-15 127 views
1

我正在使用一个已有的MVVM项目,直到现在我都做得很好。我有一个treeView代码,我想 按下时释放节点。换句话说,今天如果我点击任何节点,它不能再选择(直到你点击另一个节点)。我希望能够根据需要多次点击同一个节点。如果我双击节点,我有“IsExpended”属性,它会转到此代码部分。我看不出哪里是双击绑定,只需一次点击绑定...MVVM Treeview点击节点

这里的XAML

<UserControl x:Class="Envitech.Setup.Presentation.Views.TreeViewViews.StationTree" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:behaviour="clr-namespace:Envitech.Setup.Presentation.Extensions;assembly=Envitech.Setup.Presentation" xmlns:nodes="clr-namespace:Envitech.Setup.Presentation.ViewModels.TreeViewViewModels.Nodes" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" d:DesignHeight="226" d:DesignWidth="227" xmlns:ml="clr-namespace:Envitech.Setup.Presentation.ml_resources"> 
    <TreeView Margin="10 0 10 20" ItemsSource="{Binding Path=Root.Children}" VirtualizingStackPanel.IsVirtualizing="True" VirtualizingStackPanel.VirtualizationMode="Recycling"> 


    <TreeView.ItemContainerStyle> 
     <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/> 
      <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" /> 
     </Style> 
    </TreeView.ItemContainerStyle> 
    <TreeView.Resources> 
     <Style TargetType="{x:Type TreeViewItem}"> 
      <Setter Property="Background" Value="Transparent" /> 
      <Setter Property="HorizontalContentAlignment" Value="{Binding Path=HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> 
      <Setter Property="VerticalContentAlignment" Value="{Binding Path=VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}" /> 
      <Setter Property="Padding" Value="1,0,0,0" /> 
      <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /> 
      <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> 
      <Setter Property="behaviour:TreeViewItemBehavior.IsBroughtIntoViewWhenSelected" Value="True" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type TreeViewItem}"> 
         <Grid> 
          <Grid.ColumnDefinitions> 
           <ColumnDefinition MinWidth="19" Width="Auto" /> 
           <ColumnDefinition Width="Auto" /> 
           <ColumnDefinition Width="*" /> 
          </Grid.ColumnDefinitions> 
          <Grid.RowDefinitions> 
           <RowDefinition Height="Auto" /> 
           <RowDefinition /> 
          </Grid.RowDefinitions> 
          <ToggleButton x:Name="Expander" IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" ClickMode="Press" /> 
          <Border Grid.Column="1" x:Name="Selection_Border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Padding="{TemplateBinding Padding}"> 
           <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" x:Name="PART_Header" ContentSource="Header" /> 
          </Border> 
          <ItemsPresenter Grid.Column="1" Grid.ColumnSpan="2" Grid.Row="1" x:Name="ItemsHost" /> 
         </Grid> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsExpanded" Value="false"> 
           <Setter Property="Visibility" Value="Collapsed" TargetName="ItemsHost" /> 
          </Trigger> 
          <Trigger Property="HasItems" Value="false"> 
           <Setter Property="Visibility" Value="Hidden" TargetName="Expander" /> 
          </Trigger> 
          <Trigger Property="IsSelected" Value="true"> 
           <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" TargetName="Selection_Border" /> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" /> 
          </Trigger> 
          <MultiTrigger> 
           <MultiTrigger.Conditions> 
            <Condition Property="IsSelected" Value="true" /> 
            <Condition Property="IsSelectionActive" Value="false" /> 
           </MultiTrigger.Conditions> 
           <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" TargetName="Selection_Border" /> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}" /> 
          </MultiTrigger> 
          <Trigger Property="IsEnabled" Value="false"> 
           <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
     <!-- Style for the ToggleButton control used to expand/collapse a TreeViewItem control --> 
     <Style TargetType="{x:Type ToggleButton}"> 
      <Setter Property="Focusable" Value="False" /> 
      <Setter Property="Width" Value="19" /> 
      <Setter Property="Height" Value="13" /> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type ToggleButton}"> 
         <Border Width="19" Height="13" Background="#00FFFFFF" x:Name="Border"> 
          <Border Width="9" Height="9" x:Name="Border1" SnapsToDevicePixels="True" BorderBrush="#FF9495A2" BorderThickness="1,1,1,1" CornerRadius="1,1,1,1"> 
           <Border.Background> 
            <LinearGradientBrush StartPoint="0,0" EndPoint="1,1"> 
             <GradientStop Color="#FFFFFFFF" Offset="0.4" /> 
             <GradientStop Color="#FFC6CEDA" Offset="1" /> 
            </LinearGradientBrush> 
           </Border.Background> 
           <Path Fill="#FF000000" Margin="1,1,1,1" x:Name="ExpandPath" Data="M0,2L0,3 2,3 2,5 3,5 3,3 5,3 5,2 3,2 3,0 2,0 2,2z" /> 
          </Border> 
         </Border> 
         <ControlTemplate.Triggers> 
          <Trigger Property="IsChecked" Value="True"> 
           <Setter Property="Data" Value="M0,2L0,3 5,3 5,2z" TargetName="ExpandPath" /> 
          </Trigger> 
         </ControlTemplate.Triggers> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 
     <DataTemplate DataType="{x:Type nodes:MonitorNode}"> 
      <StackPanel Orientation="Horizontal" Margin="0 2 0 0"> 
       <Image Source="{Binding ImageSource}" Width="16" Height="16" Margin="0 0 3 0" /> 
       <TextBlock Text="{x:Static ml:MultiLang._157}" x:Name="ML_0048" /> 
       <TextBlock Text="{Binding ID}" /> 
       <TextBlock Text="{x:Static ml:MultiLang._158}" x:Name="ML_0050" /> 
       <TextBlock Text="{Binding Name}" /> 
      </StackPanel> 
     </DataTemplate> 
     <HierarchicalDataTemplate DataType="{x:Type nodes:StationNode}" ItemsSource="{Binding Children}"> 
      <StackPanel Orientation="Horizontal" Margin="0 2 0 0"> 
       <StackPanel.ContextMenu> 
        <ContextMenu HasDropShadow="True"> 
         <MenuItem Header="{x:Static ml:MultiLang._160}" Command="{Binding CopyMonitorsCommand}" x:Name="ML_0055" /> 
         <MenuItem Header="{x:Static ml:MultiLang._159}" Command="{Binding PasteMonitorsCommand}" x:Name="ML_0056" /> 
         <MenuItem Header="{x:Static ml:MultiLang._162}" Command="{Binding CopyStationsCommand}" x:Name="ML_0057" /> 
         <MenuItem Header="{x:Static ml:MultiLang._161}" Command="{Binding PasteStationsCommand}" x:Name="ML_0058" /> 
        </ContextMenu> 
       </StackPanel.ContextMenu> 
       <Image Source="{Binding ImageSource}" Width="16" Height="16" Margin="0 0 3 0" /> 
       <TextBlock Text="{x:Static ml:MultiLang._157}" x:Name="ML_0060" /> 
       <TextBlock Text="{Binding ID}" /> 
       <TextBlock Text="{x:Static ml:MultiLang._158}" x:Name="ML_0062" /> 
       <TextBlock Text="{Binding Name}" /> 
      </StackPanel> 
     </HierarchicalDataTemplate> 
     <HierarchicalDataTemplate DataType="{x:Type nodes:FilteringNode}" ItemsSource="{Binding Children}"> 
      <StackPanel Orientation="Horizontal" Margin="0 2 0 0"> 
       <Image Source="{Binding ImageSource}" Width="16" Height="16" Margin="0 0 3 0" /> 
       <TextBlock Text="{Binding Name}" /> 
      </StackPanel> 
     </HierarchicalDataTemplate> 
    </TreeView.Resources> 
    </TreeView> 
</UserControl> 

谢谢

回答

1

当你双击一个树视图项,它会扩大。展开后,您的IsExpanded属性将由于绑定而发生更改。单击当前未选中的内容将会更改您的IsSelected属性。但是,再次单击它(不是双击)将不会更改IsSelected属性。看起来你想要做的就是添加一个'Click'事件处理程序。这样,每次点击时,您都会收到通知,而不仅仅是在第一次点击时选择其他内容。

要完成此操作,请将'MouseDoubleClick'事件添加到您的TreeViewItem样式中。如果你想有一个单一的点击,只是做了同为“的MouseDown”事件:

<Style TargetType="{x:Type TreeViewItem}"> 
    <Setter Property="IsSelected" Value="{Binding Path=IsSelected, Mode=TwoWay}"/> 
    <Setter Property="IsExpanded" Value="{Binding Path=IsExpanded, Mode=TwoWay}" /> 
    <EventSetter Event="MouseDoubleClick" Handler="DoubleClickedItem"/> 
</Style> 

一旦你的XAML中它上面的代码中,右键点击单词“DoubleClickedItem”你的鼠标,选择“NavigateToHandler '选项,Visual Studio将在您的代码中为您生成一个方法存根。然后,您可以在那里实现双击行为。

+1

非常感谢柯蒂斯。你是完全正确的,但我该怎么做?我寻找双击处理,并找不到它。如果我将看到它,我会知道如何做单击... –

+0

我已经添加了一个代码示例到我原来的答案,以处理树视图项目上的双击事件。 – Curtis

+1

谢谢柯蒂斯。它不适合我。你能告诉我在我的xaml文件中的双击事件或选定的项目事件吗?我不明白是什么使它工作... –

0

这个答案解释了如何使用Interactivity扩展来完全保留MVVM。

WPF RightClick MouseBinding on release?

我已经无法使用MouseLeftButtonDown事件,但在的MouseLeftButtonUp我的情况下工作(在已选定的树型视图点击的通知)。

我是猜测 MouseLeftButtonDown是事件树中较早处理的,所以我们永远不会看到它。