WPF 水平数据网格 [英] WPF horizontal DataGrid

查看:20
本文介绍了WPF 水平数据网格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个水平方向的 WPF DataGrid,有人知道解决方案吗?

I would like to have a WPF DataGrid with a horizontal orientation, does anyone know a solution?

推荐答案

我之前已经这样做了,因为我们希望能够对 DataGridPropertyGrid 使用相同的控件.许多事情必须改变(如对齐、滚动、排序箭头的定位等).有很多代码可以发布整个解决方案,但这应该可以帮助您入门.这是自动生成的 TextColumns 的示例,但您可以轻松修改它以使用其他列类型.

I've done this earlier since we wanted to be able to use the same control for a DataGrid and a PropertyGrid. Alot of things have to be changed (like alignment, scrolling, positioning of sort-arrows etc.). There is way to much code to post the whole solution but this should get you started. This is an example with Autogenerated TextColumns but you can easily modify it to use other column types.

<ScrollViewer Name="c_dataGridScrollViewer"
              Loaded="c_dataGridScrollViewer_Loaded"
              VerticalScrollBarVisibility="Auto"
              HorizontalScrollBarVisibility="Auto">
    <DataGrid x:Name="c_dataGrid"
              HorizontalAlignment="Left"
              VerticalAlignment="Top"
              AutoGeneratedColumns="c_dataGrid_AutoGeneratedColumns"
              HorizontalScrollBarVisibility="Hidden"
              VerticalScrollBarVisibility="Hidden">
        <DataGrid.ColumnHeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="LayoutTransform">
                    <Setter.Value>
                        <TransformGroup>
                            <RotateTransform Angle="90"/>
                        </TransformGroup>
                    </Setter.Value>
                </Setter>
            </Style>
        </DataGrid.ColumnHeaderStyle>
        <DataGrid.LayoutTransform>
            <TransformGroup>
                <RotateTransform Angle="-90"/>
            </TransformGroup>
        </DataGrid.LayoutTransform>
    </DataGrid>
</ScrollViewer>

当Columns生成时,我们反转它们的位置并旋转TextBlocks和TextBoxes(这在对齐、模糊等方面比旋转DataGridCell要好)

And when the Columns are generated, we reverse their positions and rotates the TextBlocks and TextBoxes (This is better than rotating the DataGridCell in terms of alignment, blur etc.)

private void c_dataGridScrollViewer_Loaded(object sender, RoutedEventArgs e)
{
    // Add MouseWheel support for the datagrid scrollviewer.
    c_dataGrid.AddHandler(MouseWheelEvent, new RoutedEventHandler(DataGridMouseWheelHorizontal), true);
}

private void DataGridMouseWheelHorizontal(object sender, RoutedEventArgs e)
{
    MouseWheelEventArgs eargs = (MouseWheelEventArgs)e;
    double x = (double)eargs.Delta;
    double y = c_dataGridScrollViewer.VerticalOffset;
    c_dataGridScrollViewer.ScrollToVerticalOffset(y - x);
}

private void c_dataGrid_AutoGeneratedColumns(object sender, EventArgs e)
{
    TransformGroup transformGroup = new TransformGroup();
    transformGroup.Children.Add(new RotateTransform(90));
    foreach (DataGridColumn dataGridColumn in c_dataGrid.Columns)
    {
        if (dataGridColumn is DataGridTextColumn)
        {
            DataGridTextColumn dataGridTextColumn = dataGridColumn as DataGridTextColumn;

            Style style = new Style(dataGridTextColumn.ElementStyle.TargetType, dataGridTextColumn.ElementStyle.BasedOn);
            style.Setters.Add(new Setter(TextBlock.MarginProperty, new Thickness(0, 2, 0, 2)));
            style.Setters.Add(new Setter(TextBlock.LayoutTransformProperty, transformGroup));
            style.Setters.Add(new Setter(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Center));

            Style editingStyle = new Style(dataGridTextColumn.EditingElementStyle.TargetType, dataGridTextColumn.EditingElementStyle.BasedOn);
            editingStyle.Setters.Add(new Setter(TextBox.MarginProperty, new Thickness(0, 2, 0, 2)));
            editingStyle.Setters.Add(new Setter(TextBox.LayoutTransformProperty, transformGroup));
            editingStyle.Setters.Add(new Setter(TextBox.HorizontalAlignmentProperty, HorizontalAlignment.Center));

            dataGridTextColumn.ElementStyle = style;
            dataGridTextColumn.EditingElementStyle = editingStyle;
        }
    }
    List<DataGridColumn> dataGridColumns = new List<DataGridColumn>();
    foreach (DataGridColumn dataGridColumn in c_dataGrid.Columns)
    {
        dataGridColumns.Add(dataGridColumn);
    }
    c_dataGrid.Columns.Clear();
    dataGridColumns.Reverse();
    foreach (DataGridColumn dataGridColumn in dataGridColumns)
    {
        c_dataGrid.Columns.Add(dataGridColumn);
    }
}

这篇关于WPF 水平数据网格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆