gpt4 book ai didi

c# - WPF 数据网格 : reduce column width to fit its content while scrolling

转载 作者:太空狗 更新时间:2023-10-29 23:04:59 27 4
gpt4 key购买 nike

当我滚动垂直滚动条时,DataGrid 会在新可见行中的内容更大并超过之前的列宽时自动扩展列宽。没关系。

但是如果所有较大的行都被滚动并且新的可见行具有较小的内容宽度,DataGrid 不会减小列宽。有办法实现吗?

附加行为的实现会很棒。

代码行为:

 public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
var persons = new List<Person>();
for (var i = 0; i < 20; i++)
persons.Add(new Person() {Name = "Coooooooooooooool", Surname = "Super"});
for (var i = 0; i < 20; i++)
persons.Add(new Person() {Name = "Cool", Surname = "Suuuuuuuuuuuuuuper"});
for (var i = 0; i < 20; i++)
persons.Add(new Person() {Name = "Coooooooooooooool", Surname = "Super"});
DG.ItemsSource = persons;
}

public class Person
{
public string Name { get; set; }
public string Surname { get; set; }
}
}

XAML:

<Window
x:Class="WpfApp4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="400"
Height="200"
mc:Ignorable="d">
<Grid>
<DataGrid
x:Name="DG"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False" />
</Grid>
</Window>

最佳答案

添加LoadingRow Datagrid 的属性(property):

   <DataGrid x:Name="DG"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False" LoadingRow="DG_LoadingRow">
</DataGrid>

然后添加这段代码在后面的代码中:

private void DG_LoadingRow(object sender, DataGridRowEventArgs e)
{
foreach (DataGridColumn c in DG.Columns)
c.Width = 0;

DG.UpdateLayout();

foreach (DataGridColumn c in DG.Columns)
c.Width = DataGridLength.Auto;
}

这绝对不是最干净的解决方案,但它会在滚动时调整可见列的大小。

希望这对您有所帮助。


Can you please wrap this into attached behaviour?

1) 第一种选择是使用附加属性

public class DataGridHelper : DependencyObject
{
public static readonly DependencyProperty SyncedColumnWidthsProperty =
DependencyProperty.RegisterAttached(
"SyncedColumnWidths",
typeof(Boolean),
typeof(DataGridHelper),
new FrameworkPropertyMetadata(false,
FrameworkPropertyMetadataOptions.AffectsRender,
new PropertyChangedCallback(OnSyncColumnsChanged)
));

private static void OnSyncColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is DataGrid dataGrid)
{
dataGrid.LoadingRow += SyncColumnWidths;
}
}

private static void SyncColumnWidths(object sender, DataGridRowEventArgs e)
{
var dataGrid = (DataGrid)sender;

foreach (DataGridColumn c in dataGrid.Columns)
c.Width = 0;

e.Row.UpdateLayout();

foreach (DataGridColumn c in dataGrid.Columns)
c.Width = DataGridLength.Auto;
}

public static void SetSyncedColumnWidths(UIElement element, Boolean value)
{
element.SetValue(SyncedColumnWidthsProperty, value);
}
}

用法

<DataGrid
ext:DataGridHelper.SyncedColumnWidths="True"
... />

2) 或者,行为 提供了一种更加封装的方式来扩展功能(需要 System.Windows.Interactivity )。

using System.Windows.Interactivity;

...

public class SyncedColumnWidthsBehavior : Behavior<DataGrid>
{
protected override void OnAttached()
{
this.AssociatedObject.LoadingRow += this.SyncColumnWidths;
}

protected override void OnDetaching()
{
this.AssociatedObject.LoadingRow -= this.SyncColumnWidths;
}

private void SyncColumnWidths(object sender, DataGridRowEventArgs e)
{
var dataGrid = this.AssociatedObject;

foreach (DataGridColumn c in dataGrid.Columns)
c.Width = 0;

e.Row.UpdateLayout();

foreach (DataGridColumn c in dataGrid.Columns)
c.Width = DataGridLength.Auto;
}
}

用法

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

...

<DataGrid
... >
<i:Interaction.Behaviors>
<ext:SyncedColumnWidthsBehavior />
</i:Interaction.Behaviors>
</DataGrid>

行为提供了一种干净的方式来释放事件处理程序。虽然,在这种情况下,即使我们不取消订阅附加属性,我们也不会造成内存泄漏(引用 Is it bad to not unregister event handlers?)。

关于c# - WPF 数据网格 : reduce column width to fit its content while scrolling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53692672/

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