gpt4 book ai didi

WPF 横向 DataGrid

转载 作者:太空宇宙 更新时间:2023-11-03 16:48:35 30 4
gpt4 key购买 nike

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

最佳答案

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

alt text

<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>

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

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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4987103/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com