【问题标题】:DataGrid Select Entire ColumnDataGrid 选择整列
【发布时间】:2012-01-30 14:05:42
【问题描述】:

我正在使用 WPF DataGrid,我希望允许用户通过单击列标题来选择整个列。我目前将 SelectionUnit 设置为 CellOrRowHeader,因为我希望行具有相同的功能(效果很好)。有没有一种简单的方法可以通过单击列标题来选择列?

【问题讨论】:

  • 我最近遇到了同样的情况,经过几个小时的搜索,一无所获,终于用鼠标事件和循环实现了它!
  • 我在想也许可以通过覆盖列标题样式使其成为按钮,然后在该按钮上添加单击事件来完成。我想让它看起来就像行标题样式,但我是样式新手。也许如果我能弄清楚行标题样式是什么样的,我可以复制它并进行调整以用于列标题......
  • by override 如果你的意思是经典的覆盖,那么当你在 WPF 中具有巨大的样式和模板潜力时,它不是最好的解决方案。但是你的想法是正确的,如果你像这样覆盖默认的 Aero 样式可能会起作用:stackoverflow.com/questions/643440/… 但是如果你更改控件的模板,那么默认样式可能不再起作用。

标签: c# wpf datagrid


【解决方案1】:

你有很多选择。一种是为 DataGrid 的标题样式创建自己的模板。在 DataTemplate 标记内,您可以更改标题的模板。 (您可以将 Button 替换为 TextBlock 或任何您想要的。)

<DataGrid>
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader" >
            <Setter Property="Foreground" Value="Blue" />
            <Setter Property="ContentTemplate" >
                <Setter.Value>
                    <DataTemplate>
                        <Button Content={Binding Content}" MouseDown="mouseDownEventHandler">
                    </DataTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.ColumnHeaderStyle>
</DataGrid>

或者如果您使用 DataGrid.Columns 来填充您的列并且您需要分别设置它们中的每一个,您可以使用这个:

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn HeaderTemplate="{StaticResource MyTemplate1"/>
        <DataGridHyperlinkColumn HeaderTemplate="{StaticResource MyTemplate2"/>
    </DataGrid.Columns>
</DatGrid>

MyTemplate1 和 2 应在控件的资源中预先定义。


编辑:

根据this link 的另一种方法是将 PreviewMouseDown 添加到您的 DataGrid 中,然后确定鼠标按下是否发生在标题上。

这是她的事件处理程序的简单版本:

DependencyObject dep = (DependencyObject)e.OriginalSource;
while ((dep != null) && !(dep is DataGridColumnHeader))
{
    dep = VisualTreeHelper.GetParent(dep);
}
if (dep == null) return;
if (dep is DataGridColumnHeader)
{
    MessageBox.Show(((DataGridColumnHeader)dep).Content.ToString());
}

【讨论】:

  • 知道如何让它像行标题一样在鼠标悬停时以蓝色突出显示吗?
  • 我在 google 上搜索并找到了一些解决方案,可以将点击事件从列标题中取出。其中之一正在使用 ColumnReordering 事件,如下所述:stackoverflow.com/questions/3716819/…
  • 我已经更改了上面的答案。看看第二部分(在“编辑:”之后)我认为它会解决你的问题。
【解决方案2】:

你也可以修改ColumnHeaderStyle:

在 XAML 中:

<DataGrid>
    <DataGrid.ColumnHeaderStyle>
        <Style TargetType="DataGridColumnHeader">
            <EventSetter Event="Click" Handler="DataGridColumnHeader_OnClick"></EventSetter>
        </Style>
    </DataGrid.ColumnHeaderStyle>
</DataGrid>

在代码隐藏中:

private void DataGridColumnHeader_OnClick(object sender, RoutedEventArgs e)
{
    var columnHeader = sender as DataGridColumnHeader;
    if (columnHeader != null)
        {
        if (!Keyboard.IsKeyDown(Key.LeftCtrl))
        {
            dataGrid.SelectedCells.Clear();
        }

        foreach (var item in dataGrid.Items)
        {
            dataGrid.SelectedCells.Add(new DataGridCellInfo(item, columnHeader.Column));
        }
    }
}

【讨论】:

    【解决方案3】:

    在 WPF 中没有对列选择的直接支持,因此需要扩展现有的 DataGrid 并添加对列选择的自定义支持。 下面是允许用于在标题单击时选择列的代码。 要使下面的代码工作,需要进行以下设置。

    Xaml 代码:

    <local:DataGridEx SelectionUnit="CellOrRowHeader" CanUserSortColumns="False"/>
    

    C# 代码:

    public class DataGridEx : DataGrid
    {
        /// <summary>
        /// Holds the column that is selected.
        /// </summary>
        public object SelectedColumn
        {
            get { return GetValue(SelectedColumnProperty); }
            set { SetValue(SelectedColumnProperty, value); }
        }
    
        // Using a DependencyProperty as the backing store for SelectedColumn.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty SelectedColumnProperty =
            DependencyProperty.Register("SelectedColumn", typeof(object),
        typeof(DataGridEx), new PropertyMetadata(null));
        private T GetVisualParent<T>(DependencyObject child) where T : Visual
        {
            DependencyObject parent = VisualTreeHelper.GetParent(child);
            if (parent == null || parent is T)
            {
                return parent as T;
            }
            else
            {
                return GetVisualParent<T>(parent);
            }
        }
        protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
            DataGridColumnHeader dataGridColumnHeader = GetVisualParent<DataGridColumnHeader>(e.OriginalSource as DependencyObject);
            if (dataGridColumnHeader == null)
            {
                return;
            }
            if (SelectedCells != null && SelectedCells.Count > 0)
            {
                UnselectAllCells();
                SelectedCells.Clear();
            }
            SelectedColumn = dataGridColumnHeader.Column;
            foreach (var item in this.Items)
            {
                this.SelectedCells.Add(new DataGridCellInfo(item, dataGridColumnHeader.Column));
            }
            base.OnPreviewMouseDown(e);
        }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-06
      • 2011-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多