实现AllowColumnReorder

时间:2009-06-12 19:07:03

标签: c# wpf treeview

我有一个继承自TreeView的控件(来自WPF Framework的System.Windows.Controls.TreeView),它实现了一个GridViewColumnCollection来显示树旁边的列。但是现在我需要实现AllowColumnReorder以防我们不希望用户重新排序列,我该如何实现?这是TreeView的代码:

    public class TreeListView : TreeView
    {
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new TreeListViewItem();
        }

        protected override bool IsItemItsOwnContainerOverride(object item)
        {
            return item is TreeListViewItem;
        }

        #region Public Properties

        private GridViewColumnCollection _columns;

        public GridViewColumnCollection Columns
        {
            get
            {
                if (_columns == null)
                {
                    _columns = new GridViewColumnCollection();
                }

                return _columns;
            }
        }

        public bool AllowColumnReorder { get; set; }

        #endregion
    }

谢谢!

2 个答案:

答案 0 :(得分:1)

找到解决方案。它不完全在类实现中,但是在XAML中使用GridViewHeaderRowPresenter.AllowsColumnReorder:

<Style TargetType="{x:Type controls:TreeListView}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type controls:TreeListView}">
                        <Border BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">
                            <DockPanel>
                                <GridViewHeaderRowPresenter AllowsColumnReorder="False" Columns="{Binding Path=Columns,RelativeSource={RelativeSource TemplatedParent}}"
                                            DockPanel.Dock="Top"/>
                                <ItemsPresenter/>
                            </DockPanel>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

答案 1 :(得分:0)

确定列是否应允许排序应由实际显示列数据的控件进行。例如,如果您使用GridView显示GridViewColumnCollection中找到的数据,那么您可能希望将GridView的AllowSorting属性设置为AllowColumnReorder属性中的值。