简体   繁体   中英

When extending treeview, do I need to specify a control template or does it use the default control template

Starting to make a custom treeview and I was wondering why I can't seem to get it to display. I didn't change my xaml except for replacing treeview with MultiSelectTreeView, yet it doesn't display. If I extend treeview, does the extended class inherit the default control template of the parent class?

public class MultiSelectTreeView:TreeView
  {
    #region Data Members

    private TreeViewItem LastItem = null;

    private ObservableCollection<TreeViewItem> SelectedTreeViewItemsList = new ObservableCollection<TreeViewItem>();
    public ObservableCollection<TreeViewItem> SelectedNodes
    {
      get
      {
        return SelectedTreeViewItemsList;
      }
      private set
      {

        SelectedTreeViewItemsList.Clear();
        SelectedTreeViewItemsList = value;
      }
    }

    public bool CtrlDown
    {
      get
      {
        return Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl);
      }
    }
    public bool ShiftDown
    {
      get
      {
        return Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift);
      }
    }
    #endregion Data Members


    #region Constructors
        /// <summary>
        /// Static Constructor defines the key used to find the default theme style for this control
        /// </summary>
        static MultiSelectTreeView()
        {
          DefaultStyleKeyProperty.OverrideMetadata(
                  typeof(MultiSelectTreeView), new FrameworkPropertyMetadata(typeof(MultiSelectTreeView)));
        }
        public MultiSelectTreeView()
        {
          this.SelectedItemChanged += new RoutedPropertyChangedEventHandler<object>(MultiSelectTreeView_SelectedItemChanged);
          this.Background = Brushes.Blue;
          this.Foreground = Brushes.Yellow;
        }

By default, it would use the default style for TreeView , but you specified a different DefaultStyleKey in the static constructor:

      DefaultStyleKeyProperty.OverrideMetadata(
              typeof(MultiSelectTreeView), new FrameworkPropertyMetadata(typeof(MultiSelectTreeView)));

If you do this, you must also create a style with {x:Type MultiSelectTreeView} as the key.

If you just want the default style, remove that statement from your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM