Disable WPF TreeView (or TreeViewItem) selection?

2019-06-28 05:12发布

Is there a nice way (except retemplating the whole TreeViewItem.Template) to disable selection in TreeView?

I am basically looking for the ItemsControl style of the TreeView (An ItemsControl is the best use to 'disable' selection on ListBox, read this post)

7条回答
相关推荐>>
2楼-- · 2019-06-28 05:17

I did this a differently than the accepted answer:

Lets say that you have a property in your ViewModel (say 'ShouldPreventSelection') Now when ShouldPreventSelection is true you want selection to be disabled:

In your TreeView fire the PreviewSelected event like so:

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

Then in the codebehind you can the following:

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

}
查看更多
我想做一个坏孩纸
3楼-- · 2019-06-28 05:32

Whenever an item is selected, you could "unselect" it. Ex. modify the code from http://www.codeproject.com/KB/WPF/TreeView_SelectionWPF.aspx or use a MVVM approach (see http://www.codeproject.com/KB/WPF/TreeViewWithViewModel.aspx) and always set IsSelected back to false.

查看更多
何必那么认真
4楼-- · 2019-06-28 05:32

Try this:

<Trigger Property="HasItems" Value="true">
   <Setter Property="Focusable" Value="false" />
</Trigger>
查看更多
对你真心纯属浪费
5楼-- · 2019-06-28 05:33

I just unselected the TreeViewItems as they get selected. I Use the TreeView only once. However if i added several placed I would consider looking into adding this to a Attacked Behavior.

Xaml:

<TreeView SelectedItemChanged="TreeView_SelectionChanged">

Code behind:

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;
}
查看更多
我想做一个坏孩纸
6楼-- · 2019-06-28 05:36

Based off of the links to the currently accepted answer, I implemented this in my project:

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

Works for TreeViewItem as well. And in the view model:

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

Now you don't have to go hunting!

查看更多
Melony?
7楼-- · 2019-06-28 05:39

This did the trick for me (based on this answer, but no tied to item - selection is disabled whatsoever):

<TreeView>
  <TreeView.ItemContainerStyle>
    <Style TargetType="TreeViewItem">
      <Setter Property="Focusable" Value="False" />
    </Style>
  </TreeView.ItemContainerStyle>
</TreeView>
查看更多
登录 后发表回答