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

问题描述

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

解决方案

因为我们希望能够为 DataGrid PropertyGrid 使用相同的控件。许多事情必须改变(如对齐,滚动,排序箭头的定位等)。有很多代码可以发布整个解决方案,但这应该让你开始。这是Autogenerated TextColumns的示例,但您可以轻松地修改它以使用其他列类型。



 < ScrollViewer Name =c_dataGridScrollViewer
Loaded = c_dataGridScrollViewer_Loaded
VerticalScrollBarVisibility =Auto
Horizo​​ntalScrollBarVisibility =Auto>
< DataGrid x:Name =c_dataGrid
Horizo​​ntalAlignment =Left
VerticalAlignment =Top
AutoGeneratedColumns =c_dataGrid_AutoGeneratedColumns
Horizo​​ntalScrollBarVisibility =隐藏
VerticalScrollBarVisibility =隐藏>
< 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>

当生成列时,我们反转位置并旋转TextBlocks和TextBoxes(这更好而不是按照对齐方式,模糊等来旋转 DataGridCell

  private void c_dataGridScrollViewer_Loaded(object sender,RoutedEventArgs e)
{
//为Datagrid scrollviewer添加MouseWheel支持。
c_dataGrid.AddHandler(MouseWheelEvent,new RoutedEventHandler(DataGridMouseWheelHorizo​​ntal),true);
}

private void DataGridMouseWheelHorizo​​ntal(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是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.Horizo​​ntalAlignmentProperty,Horizo​​ntalAlignment.Center));

样式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(新的Setter(TextBox.Horizo​​ntalAlignmentProperty,Horizo​​ntalAlignment.Center));

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


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

解决方案

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>

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水平DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:47