禁用WPF TreeView(或TreeViewItem)选择?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了禁用WPF TreeView(或TreeViewItem)选择?相关的知识,希望对你有一定的参考价值。

有没有一个很好的方法(除了重新模拟整个TreeViewItem.Template)禁用TreeView中的选择?

我基本上是在寻找ItemsControlTreeView风格(ItemsControl最好用'禁用'ListBox上的选择,阅读this帖子)

答案

每当选择一个项目时,您可以“取消选择”它。防爆。修改http://www.codeproject.com/KB/WPF/TreeView_SelectionWPF.aspx中的代码或使用MVVM方法(请参阅http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx)并始终将IsSelected设置为false。

另一答案

试试这个:

<Trigger Property="HasItems" Value="true">
   <Setter Property="Focusable" Value="false" />
</Trigger>
另一答案

基于当前接受的答案的链接,我在我的项目中实现了这个:

<ListView.ItemContainerStyle>
    <Style TargetType="{x:Type ListViewItem}">
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
    </Style>
</ListView.ItemContainerStyle>

也适用于TreeViewItem。并在视图模型中:

protected bool _DisableSelection;
private bool _IsSelected;
public bool IsSelected
{
    get { return _IsSelected; }
    set
    {
        if (value == _IsSelected) return;
        _IsSelected = _DisableSelection ? false : value;
        NotifyPropertyChanged();
    }
}

现在你不必去打猎了!

另一答案

这对我来说很有用(基于this答案,但没有与项目相关 - 选择被禁用):

<TreeView>
  <TreeView.ItemContainerStyle>
    <Style TargetType="TreeViewItem">
      <Setter Property="Focusable" Value="False" />
    </Style>
  </TreeView.ItemContainerStyle>
</TreeView>
另一答案

我决定写一个可重用的行为,HTH:

Namespace Components
  Public NotInheritable Class TreeViewBehavior

    Public Shared Function GetIsTransparent(
      ByVal element As TreeViewItem) As Boolean
      If element Is Nothing Then Throw New ArgumentNullException("element")
      Return element.GetValue(IsTransparentProperty)
    End Function
    Public Shared Sub SetIsTransparent(ByVal element As TreeViewItem,
                                       ByVal value As Boolean)
      If element Is Nothing Then Throw New ArgumentNullException("element")
      element.SetValue(IsTransparentProperty, value)
    End Sub
    Public Shared ReadOnly IsTransparentProperty As DependencyProperty =
      DependencyProperty.RegisterAttached("IsTransparent", GetType(Boolean),
        GetType(TreeViewBehavior),
        New FrameworkPropertyMetadata(False,
          AddressOf IsTransparent_PropertyChanged))
    Private Shared Sub IsTransparent_PropertyChanged(
      ByVal sender As Object, ByVal e As DependencyPropertyChangedEventArgs)
      Dim tvi = DirectCast(sender, TreeViewItem)
      Dim isTransparent = CBool(e.NewValue)

      If isTransparent Then
        AddHandler tvi.Selected, AddressOf tvi_Selected
      Else
        RemoveHandler tvi.Selected, AddressOf tvi_Selected
      End If
    End Sub
    Private Shared Sub tvi_Selected(ByVal sender As Object,
                                    ByVal e As RoutedEventArgs)
      Dim treeViewItem = DirectCast(sender, TreeViewItem)
      If Not treeViewItem.IsSelected Then Exit Sub

      treeViewItem.Dispatcher.Invoke(
        Sub(tvi As TreeViewItem) tvi.IsSelected = False,
        System.Windows.Threading.DispatcherPriority.Send,
        treeViewItem)
    End Sub

  End Class
End Namespace

用法:

<Window xmlns:components="clr-namespace:WpfApplication.Components">
  <TreeView>
    <TreeView.ItemContainerStyle>
      <Style TargetType="TreeViewItem">
        <Setter 
          Property="components:TreeViewBehavior.IsTransparent" 
          Value="True" />
      </Style>
    </TreeView.ItemContainerStyle>
  </TreeView>
</Window> 
另一答案

我刚刚选中TreeViewItems时取消选择它。我只使用TreeView一次。但是,如果我添加了几个放置,我会考虑将其添加到攻击行为中。

XAML:

<TreeView SelectedItemChanged="TreeView_SelectionChanged">

代码背后:

private void TreeView_SelectionChanged(object sender, RoutedEventArgs e)
{
    var myTreeView = sender as TreeView;
    if (myTreeView == null) return;
    var selectedItem = (TreeViewItem)myTreeView.SelectedItem;
    if (selectedItem ==null) return;
    selectedItem.IsSelected = false;
}
另一答案

我这样做的方式与接受的答案不同:

假设您在ViewModel中有一个属性(比如'ShouldPreventSelection')现在,当Pre Prevent Selection为true时,您希望禁用选择:

在您的TreeView中触发PreviewSelected事件,如下所示:

<TreeView Name="TreeView1"
     ...
     PreviewSelected="TreeView1_PreviewSelected"
     ..
/>

然后在代码隐藏中,您可以执行以下操作:

private void TreeView1_PreviewSelected(object sender, RoutedEventArgs e)
{
    MyViewModel myViewModel = TreeView1.DataContext as MyViewModel;
    if (myViewModel == null)
    {
        return;
    }
    if (myViewModel .ShouldPreventSelection)
    {
        e.Handled = true;
    }

}

以上是关于禁用WPF TreeView(或TreeViewItem)选择?的主要内容,如果未能解决你的问题,请参考以下文章

在 WPF 设置中 DragOver 中的 e.Effects 不起作用 - 为啥?

滚动时禁用拖放

TreeView SelectedItem 返回类型

如何在 TreeView 中禁用或修改 TreeCell 的填充

WPF MVVM TreeView SelectedItem

c# treeview 字体大小