3

开始制作自定义树视图,我想知道为什么我似乎无法显示它。除了用 MultiSelectTreeView 替换 treeview 之外,我没有更改我的 xaml,但它没有显示。如果我扩展treeview,扩展类是否继承父类的默认控件模板?

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;
        }
4

1 回答 1

4

默认情况下,它将使用默认样式,但您在静态构造函数中TreeView指定了不同的样式:DefaultStyleKey

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

如果这样做,您还必须创建一个以{x:Type MultiSelectTreeView}作为键的样式。

如果您只想要默认样式,请从代码中删除该语句。

于 2013-08-13T22:10:49.607 回答