我對調整列大小的默認行為有疑問:
如果 DataGrid 對于其容器來說太寬,則會出現水平滾動條。如果我向右拖動欄并調整最右側列的大小,則滾動條會向右移動。
就我而言,我不想要這種行為。滾動條要么不貼在右邊,要么像 MS Excel 一樣調整大小預覽。
有人可以告訴我如何實作這一目標嗎?
Edit1:這種行為很好(不堅持正確):

我不喜歡的是這個:

如果我能很容易地意識到這一點,我寧愿:

/編輯1
我將 .Net 4.8 用于一個簡單的 WPF 應用程式。
如果需要一個例子,下面將顯示兩個網格,左邊的一個可用于該行為:
<Window x:Class="DataGridTest.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"
xmlns:local="clr-namespace:DataGridTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.DataContext>
<local:MasterViewModel/>
</Window.DataContext>
<DockPanel>
<Button DockPanel.Dock="Bottom" Command="{Binding DisplaySelectionCountCommand}">Display Selection Count</Button>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<DataGrid Grid.Column="0" ItemsSource="{Binding Items}" AutoGenerateColumns="False"
SelectionMode="Extended" local:MultiSelect.IsEnabled="True" HorizontalScrollBarVisibility="Auto">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="100"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="100"/>
<DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="100"/>
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.Column="1" ItemsSource="{Binding Items}"
SelectionMode="Extended" local:MultiSelect.IsEnabled="True"/>
</Grid>
</DockPanel>
</Window>
uj5u.com熱心網友回復:
當我建議收聽SizeChanged事件時,我并不是說DataGrid要成為訊息來源。
既然你對列感興趣,你當然必須聽單元格事件:
主視窗.xaml
<DataGrid>
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="SizeChanged" Handler="DataGridCell_SizeChanged" />
</Style>
</DataGrid.CellStyle>
</DataGrid>
主視窗.xaml.cs
private void DataGridCell_SizeChanged(object sender, SizeChangedEventArgs e)
=> (sender as DataGridCell).BringIntoView();
這是另一種版本,展示了如何使用獲得對滾動位置的更多控制ScrollViewer的DataGrid:
主視窗.xaml
<DataGrid x:Name="DataGrid"
ScrollViewer.ScrollChanged="DataGrid_ScrollChanged">
<DataGrid.CellStyle>
<Style TargetType="DataGridCell">
<EventSetter Event="SizeChanged" Handler="DataGridCell_SizeChanged" />
</Style>
</DataGrid.CellStyle>
</DataGrid>
主視窗.xaml.cs
private double OriginalScrollPosition { get; set; }
private bool IsResizingColumn { get; set; }
private void DataGrid_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
var dataGrid = sender as DataGrid;
if (this.IsResizingColumn
&& TryFindVisualChildElement(dataGrid, out ScrollViewer scrollViewer))
{
this.Dispatcher.InvokeAsync(() =>
{
scrollViewer.ScrollToHorizontalOffset(this.OriginalScrollPosition);
this.IsResizingColumn = false;
}, DispatcherPriority.Background);
}
}
private void DataGrid_SizeChanged(object sender, SizeChangedEventArgs e)
{
var dataGridCell = sender as DataGridCell;
if (TryFindVisualChildElement(this.DataGrid, out ScrollViewer scrollViewer))
{
this.OriginalScrollPosition = scrollViewer.HorizontalOffset;
this.IsResizingColumn = true;
}
}
private bool TryFindVisualChildElement<TChild>(DependencyObject parent, out TChild resultElement)
where TChild : DependencyObject
{
resultElement = null;
if (parent is Popup popup)
{
parent = popup.Child;
if (parent == null)
{
return false;
}
}
for (var childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(parent); childIndex )
{
DependencyObject childElement = VisualTreeHelper.GetChild(parent, childIndex);
if (childElement is TChild child)
{
resultElement = child;
return true;
}
if (TryFindVisualChildElement(childElement, out resultElement))
{
return true;
}
}
return false;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/334478.html
