Data binding to SelectedItem in a WPF Treeview

后端 未结 20 766
南方客
南方客 2020-11-22 05:39

How can I retrieve the item that is selected in a WPF-treeview? I want to do this in XAML, because I want to bind it.

You might think that it is SelectedItem

相关标签:
20条回答
  • 2020-11-22 06:38

    This can be accomplished in a 'nicer' way using only binding and the GalaSoft MVVM Light library's EventToCommand. In your VM add a command which will be called when the selected item is changed, and initialize the command to perform whatever action is necessary. In this example I used a RelayCommand and will just set the SelectedCluster property.

    public class ViewModel
    {
        public ViewModel()
        {
            SelectedClusterChanged = new RelayCommand<Cluster>( c => SelectedCluster = c );
        }
    
        public RelayCommand<Cluster> SelectedClusterChanged { get; private set; } 
    
        public Cluster SelectedCluster { get; private set; }
    }
    

    Then add the EventToCommand behavior in your xaml. This is really easy using blend.

    <TreeView
          x:Name="lstClusters"
          ItemsSource="{Binding Path=Model.Clusters}" 
          ItemTemplate="{StaticResource HoofdCLusterTemplate}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectedItemChanged">
                <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding SelectedClusterChanged}" CommandParameter="{Binding ElementName=lstClusters,Path=SelectedValue}"/>
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </TreeView>
    
    0 讨论(0)
  • 2020-11-22 06:38

    There is also a way to create XAML bindable SelectedItem property without using Interaction.Behaviors.

    public static class BindableSelectedItemHelper
    {
        #region Properties
    
        public static readonly DependencyProperty SelectedItemProperty = DependencyProperty.RegisterAttached("SelectedItem", typeof(object), typeof(BindableSelectedItemHelper),
            new FrameworkPropertyMetadata(null, OnSelectedItemPropertyChanged));
    
        public static readonly DependencyProperty AttachProperty = DependencyProperty.RegisterAttached("Attach", typeof(bool), typeof(BindableSelectedItemHelper), new PropertyMetadata(false, Attach));
    
        private static readonly DependencyProperty IsUpdatingProperty = DependencyProperty.RegisterAttached("IsUpdating", typeof(bool), typeof(BindableSelectedItemHelper));
    
        #endregion
    
        #region Implementation
    
        public static void SetAttach(DependencyObject dp, bool value)
        {
            dp.SetValue(AttachProperty, value);
        }
    
        public static bool GetAttach(DependencyObject dp)
        {
            return (bool)dp.GetValue(AttachProperty);
        }
    
        public static string GetSelectedItem(DependencyObject dp)
        {
            return (string)dp.GetValue(SelectedItemProperty);
        }
    
        public static void SetSelectedItem(DependencyObject dp, object value)
        {
            dp.SetValue(SelectedItemProperty, value);
        }
    
        private static bool GetIsUpdating(DependencyObject dp)
        {
            return (bool)dp.GetValue(IsUpdatingProperty);
        }
    
        private static void SetIsUpdating(DependencyObject dp, bool value)
        {
            dp.SetValue(IsUpdatingProperty, value);
        }
    
        private static void Attach(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            TreeListView treeListView = sender as TreeListView;
            if (treeListView != null)
            {
                if ((bool)e.OldValue)
                    treeListView.SelectedItemChanged -= SelectedItemChanged;
    
                if ((bool)e.NewValue)
                    treeListView.SelectedItemChanged += SelectedItemChanged;
            }
        }
    
        private static void OnSelectedItemPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            TreeListView treeListView = sender as TreeListView;
            if (treeListView != null)
            {
                treeListView.SelectedItemChanged -= SelectedItemChanged;
    
                if (!(bool)GetIsUpdating(treeListView))
                {
                    foreach (TreeViewItem item in treeListView.Items)
                    {
                        if (item == e.NewValue)
                        {
                            item.IsSelected = true;
                            break;
                        }
                        else
                           item.IsSelected = false;                        
                    }
                }
    
                treeListView.SelectedItemChanged += SelectedItemChanged;
            }
        }
    
        private static void SelectedItemChanged(object sender, RoutedEventArgs e)
        {
            TreeListView treeListView = sender as TreeListView;
            if (treeListView != null)
            {
                SetIsUpdating(treeListView, true);
                SetSelectedItem(treeListView, treeListView.SelectedItem);
                SetIsUpdating(treeListView, false);
            }
        }
        #endregion
    }
    

    You can then use this in your XAML as:

    <TreeView  helper:BindableSelectedItemHelper.Attach="True" 
               helper:BindableSelectedItemHelper.SelectedItem="{Binding SelectedItem, Mode=TwoWay}">
    
    0 讨论(0)
提交回复
热议问题