TreeView.cs source code in C# .NET

小林野夫發表於2024-04-18

Code:

   / Dotnetfx_Win7_3.5.1 / Dotnetfx_Win7_3.5.1 / 3.5.1 / DEVDIV / depot / DevDiv / releases / Orcas / NetFXw7 / wpf / src / Framework / System / Windows / Controls / TreeView.cs / 1 / TreeView.cs
                        

                            //---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//--------------------------------------------------------------------------- 

using System; 
using System.Collections; 
using System.Collections.Generic;
using System.Collections.Specialized; 
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Automation.Peers; 
using System.Windows.Controls.Primitives;
using System.Windows.Data; 
using System.Windows.Input; 
using System.Windows.Media;
using MS.Internal; 
using MS.Internal.Data;
using MS.Internal.KnownBoxes;

namespace System.Windows.Controls 
{
    /// 
 
    ///     A control that presents items in a tree structure. 
    /// 

    [StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(TreeViewItem))] 
    public class TreeView : ItemsControl
    {
        #region Constructors
 
        static TreeView()
        { 
            DefaultStyleKeyProperty.OverrideMetadata(typeof(TreeView), new FrameworkPropertyMetadata(typeof(TreeView))); 
            VirtualizingStackPanel.IsVirtualizingProperty.OverrideMetadata(typeof(TreeView), new FrameworkPropertyMetadata(BooleanBoxes.FalseBox));
            _dType = DependencyObjectType.FromSystemTypeInternal(typeof(TreeView)); 

            KeyboardNavigation.DirectionalNavigationProperty.OverrideMetadata(typeof(TreeView), new FrameworkPropertyMetadata(KeyboardNavigationMode.Contained));
            KeyboardNavigation.TabNavigationProperty.OverrideMetadata(typeof(TreeView), new FrameworkPropertyMetadata(KeyboardNavigationMode.None));
        } 

        /// 
 
        ///     Creates an instance of this control. 
        /// 

        public TreeView() 
        {
            _focusEnterMainFocusScopeEventHandler = new EventHandler(OnFocusEnterMainFocusScope);
            KeyboardNavigation.Current.FocusEnterMainFocusScope += _focusEnterMainFocusScopeEventHandler;
        } 

        #endregion 
 
        #region Public Properties
 
        private static readonly DependencyPropertyKey SelectedItemPropertyKey =
            DependencyProperty.RegisterReadOnly("SelectedItem", typeof(object), typeof(TreeView), new FrameworkPropertyMetadata((object)null));

        /// 
 
        ///     The DependencyProperty for the  property.
        ///     Default Value: null 
        /// 
 
        public static readonly DependencyProperty SelectedItemProperty = SelectedItemPropertyKey.DependencyProperty;
 
        /// 

        ///     Specifies the selected item.
        /// 

        [Bindable(true), Category("Appearance"), ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
        public object SelectedItem
        { 
            get 
            {
                return GetValue(SelectedItemProperty); 
            }
        }

        private void SetSelectedItem(object data) 
        {
            if (SelectedItem != data) 
            { 
                SetValue(SelectedItemPropertyKey, data);
            } 
        }

        private static readonly DependencyPropertyKey SelectedValuePropertyKey =
            DependencyProperty.RegisterReadOnly("SelectedValue", typeof(object), typeof(TreeView), new FrameworkPropertyMetadata((object)null)); 

        /// 
 
        ///     The DependencyProperty for the  property. 
        ///     Default Value: null
        /// 
 
        public static readonly DependencyProperty SelectedValueProperty = SelectedValuePropertyKey.DependencyProperty;

        /// 

        ///     Specifies the a value on the selected item as defined by . 
        /// 

        [Bindable(true), Category("Appearance"), ReadOnly(true), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
        public object SelectedValue 
        {
            get 
            {
                return GetValue(SelectedValueProperty);
            }
        } 

        private void SetSelectedValue(object data) 
        { 
            if (SelectedValue != data)
            { 
                SetValue(SelectedValuePropertyKey, data);
            }
        }
 
        /// 

        ///     The DependencyProperty for the  property. 
        ///     Default Value: String.Empty 
        /// 

        public static readonly DependencyProperty SelectedValuePathProperty = 
            DependencyProperty.Register(
                    "SelectedValuePath",
                    typeof(string),
                    typeof(TreeView), 
                    new FrameworkPropertyMetadata(
                            String.Empty, 
                            new PropertyChangedCallback(OnSelectedValuePathChanged))); 

        /// 
 
        ///     Specifies the path to query on  to calculate .
        /// 

        [Bindable(true), Category("Appearance")]
        public string SelectedValuePath 
        {
            get { return (string) GetValue(SelectedValuePathProperty); } 
            set { SetValue(SelectedValuePathProperty, value); } 
        }
 
        private static void OnSelectedValuePathChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            TreeView tree = (TreeView)d;
            SelectedValuePathBindingExpression.ClearValue(tree); 
            tree.UpdateSelectedValue(tree.SelectedItem);
        } 
 
        #endregion
 
        #region Public Events

        /// 

        ///     Event fired when  changes. 
        /// 

        public static readonly RoutedEvent SelectedItemChangedEvent = EventManager.RegisterRoutedEvent("SelectedItemChanged", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler

相關文章