gpt4 book ai didi

c# - WPF Datagrid 列宽最大化时调整大小

转载 作者:太空宇宙 更新时间:2023-11-03 12:57:47 24 4
gpt4 key购买 nike

我在网格列中添加了 WPF 数据网格,这里是 XAML

<DataGrid x:Name="grdProgramList" IsReadOnly="True" 
Grid.Column="1" Grid.Row="1"
Width="{Binding Path=ActualWidth, ElementName=grid}"
RenderOptions.ClearTypeHint="Enabled"
TextOptions.TextFormattingMode="Display"
HeadersVisibility="All"
Margin="5"
SelectionMode="Single"
SelectionUnit="FullRow"
SelectionChanged="AzureDataGrid_Selected"
ItemsSource="{Binding Path=Uninstaller.ProgramCollection}"
SelectedItem="{Binding Uninstaller.SelectedProgramDetails, Mode=TwoWay}"
AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="PROGRAM NAME"
Binding="{Binding ProgramDetails.ProgramName}"
MinWidth="325" />
<DataGridTextColumn Header="Publisher"
Binding="{Binding ProgramDetails.PublisherName}" />
<DataGridTextColumn Header="Estimated Size (KB)"
Binding="{Binding ProgramDetails.EstimatedSize}" />
<DataGridTextColumn Header="Version"
Binding="{Binding ProgramDetails.Version}" />
</DataGrid.Columns>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="MouseDoubleClick" Handler="Row_DoubleClick"/>
<Style.Triggers>
<!-- IsSelected -->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsSelected, RelativeSource={RelativeSource Self}}" Value="true" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#CC119EDA" />
<Setter Property="Foreground" Value="White" />
<Setter Property="BorderBrush" Value="#CC119EDA" />
</MultiDataTrigger>
<!-- IsHover -->
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Path=IsMouseOver, RelativeSource={RelativeSource Self}}" Value="true" />
</MultiDataTrigger.Conditions>
<Setter Property="Background" Value="#66119EDA" />
<Setter Property="BorderBrush" Value="#66119EDA" />
</MultiDataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
</DataGrid>

当它调整大小时,我希望所有列的宽度都适合网格宽度

现在看起来像这样。

enter image description here

最佳答案

您应该设置 DataGridColumnWidth 属性。它的类型是 DataGridLength ,它允许您按比例设置值。
您可以通过将值设置为 number 后跟 *(例如 2** 默认值)来执行此操作到 1*).
如果您希望每个 column 具有相同的 width,请在每个 * 上设置它:

<DataGrid.Columns>
<DataGridTextColumn Header="PROGRAM NAME"
Binding="{Binding ProgramDetails.ProgramName}"
MinWidth="325" Width="*" />
<DataGridTextColumn Header="Publisher"
Binding="{Binding ProgramDetails.PublisherName}" Width="*" />
<DataGridTextColumn Header="Estimated Size (KB)"
Binding="{Binding ProgramDetails.EstimatedSize}" Width="*"/>
<DataGridTextColumn Header="Version"
Binding="{Binding ProgramDetails.Version}" Width="*" />
</DataGrid.Columns>

如果您只希望最后一列占据剩余空间,请仅在一列上设置 Width:

 <DataGrid.Columns>
<DataGridTextColumn Header="PROGRAM NAME"
Binding="{Binding ProgramDetails.ProgramName}"
MinWidth="325" />
<DataGridTextColumn Header="Publisher"
Binding="{Binding ProgramDetails.PublisherName}" />
<DataGridTextColumn Header="Estimated Size (KB)"
Binding="{Binding ProgramDetails.EstimatedSize}" />
<DataGridTextColumn Header="Version"
Binding="{Binding ProgramDetails.Version}" Width="*" />
</DataGrid.Columns>

关于c# - WPF Datagrid 列宽最大化时调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33315121/

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