gpt4 book ai didi

c# - 如何使 WPF 数据网格列从一开始就自动调整大小,以便它们都可见?

转载 作者:太空狗 更新时间:2023-10-29 19:38:26 26 4
gpt4 key购买 nike

我有一个数据网格,其中有一列(最后一列)应该在有空间时始终显示。不幸的是,我无法做到这一点,也无法在任何地方找到解决方案。

此 WPF 应用程序可用于纵向模式和横向模式...

当以纵向模式启动应用程序时,我希望数据网格始终显示最后一列,但最初它隐藏了几列。但是,当通过横向调整应用程序大小时(获得更大的宽度),然后返回纵向模式,它确实可以按预期工作。

大多数列的宽度设置为 Auto 或 *。只有最后一列的最小宽度为 80 像素因为我不想调整它的大小,所以它应该始终显示。

这里是网格的设置:

            <DataGrid Grid.Row="1"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding Path=MyCollection,
Mode=OneWay}"
RowDetailsVisibilityMode="Collapsed"
RowHeaderStyle="{StaticResource ExpanderRowHeaderStyle}"
SelectionMode="Single"
SelectionUnit="Cell">

这是纵向模式的初始情况 Initial image

然后当我调整为横向时:

landscape mode

最后当我回到纵向模式时,我得到了想要的结果: enter image description here

为了使我的初始情况看起来像图 3...我可以更改什么...以便所有列都从头开始显示(无需调整大小)?

编辑:为清楚起见:当我说纵向和横向模式时,我的意思是该软件用于 Windows 平板电脑,但同样的问题也会出现在常规屏幕上。当窗口最初很小时,并非所有列都显示,但是当我放大窗口然后调整回初始小尺寸时,所有列都会显示。

编辑 2:

如果我将第一列的宽度更改为 * 而不是 Auto,我就可以解决这个问题......虽然我不知道为什么......这是完整的 DataGrid

                        <DataGrid Grid.Row="1"
AutoGenerateColumns="False"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserReorderColumns="False"
CanUserResizeColumns="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
HorizontalScrollBarVisibility="Disabled"
ItemsSource="{Binding Path=BstCollection,
Mode=OneWay}"
RowDetailsVisibilityMode="Collapsed"
RowHeaderStyle="{StaticResource ExpanderRowHeaderStyle}"
SelectionMode="Single"
SelectionUnit="Cell"
EnableRowVirtualization="False" Loaded="DataGrid_Loaded" DataContextChanged="DataGrid_DataContextChanged" ColumnWidth="*">
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource IsLastOrderToBooleanMultiConverter}">
<MultiBinding.Bindings>
<Binding Mode="OneWay"
Path="BstCollection[0].BesbstldOp"
Source="{StaticResource BstGbrModel}" />
<!-- ReSharper disable Xaml.BindingWithContextNotResolved -->
<Binding Mode="OneWay" Path="BesbstldOp" />
<Binding Mode="OneWay" Path="BesHoeveelheid" />
<!-- ReSharper restore Xaml.BindingWithContextNotResolved -->
</MultiBinding.Bindings>
</MultiBinding>
</DataTrigger.Binding>
<Setter Property="Background" Value="PowderBlue" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGrid.RowStyle>
<DataGrid.Columns>
<DataGridTemplateColumn Width="Auto" Header="{specialLocalization:Translate CtlBstGbr--grdBsten--atlOmschrijving}" IsReadOnly="True">
<DataGridTemplateColumn.CellEditingTemplate >
<DataTemplate DataType="bl:Bst">
<TextBlock Text="{Binding atlOmschrijving, Mode=OneWay}" IsEnabled="False" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
<DataGridTemplateColumn.CellTemplate >
<DataTemplate DataType="bl:Bst">
<TextBlock >
<Run Text="{Binding atlOmschrijving, Mode=OneWay}"/>
<Run Foreground="Red" Text="{Binding Path=atl.atlVKMat, Mode=OneWay, Converter={StaticResource BooleanToBstTextConverter}}" />
</TextBlock>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>

</DataGridTemplateColumn>
<DataGridTextColumn Width="Auto"
Binding="{Binding Path=BesbstldOp,
Mode=OneWay,
Converter={StaticResource DateToStringValueConverter}}"
Header="{specialLocalization:Translate CtlBstGbr--grdBsten--BesbstldOp}"
IsReadOnly="True" />
<DataGridTextColumn Width="Auto"
Binding="{Binding Path=BesGeleverdOp,
Mode=OneWay,
Converter={StaticResource DateToStringValueConverter}}"
Header="{specialLocalization:Translate CtlBstGbr--grdBsten--BesGeleverdOp}"
IsReadOnly="True" />
<DataGridTextColumn Width="*"
Binding="{Binding Path=BesHoeveelheid,
Mode=OneWay,
Converter={StaticResource IntToStringValueConverter}}"
Header="{specialLocalization:Translate CtlBstGbr--grdBsten--BesHoeveelheid}"
IsReadOnly="True" />
<DataGridTextColumn Width="Auto"
Binding="{Binding Path=atlbstleenheid,
Mode=OneWay}"
Header="{specialLocalization:Translate CtlBstGbr--grdBsten--atlbstleenheid}"
IsReadOnly="True" />
<DataGridTextColumn Width="*"
Binding="{Binding Path=vpeNaam,
Mode=OneWay}"
Header="{specialLocalization:Translate CtlBstGbr--grdBsten--vpeNaam}"
IsReadOnly="True" />
<DataGridTemplateColumn Width="*" MinWidth="85" Header="{specialLocalization:Translate CtlBstGbr--grdBsten--NHoevheid}" >
<DataGridTemplateColumn.CellTemplate >
<DataTemplate DataType="bl:Bst">
<TextBlock Text="{Binding NHoevheid, Mode=OneWay}" IsEnabled="{Binding atl.atlVKMat, Mode=OneWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate DataType="bl:Bst">
<customControls:UpDownTextBox MinValue="0" Text="{Binding NHoevheid, Mode=TwoWay}" Visibility="{Binding Path=atl.atlVKMat, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
</DataGridTemplateColumn>


</DataGrid.Columns>
<DataGrid.RowDetailsTemplate>
<DataTemplate DataType="bl:Bst">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0"
Grid.Column="0"
Content="{specialLocalization:Translate CtlBstGbr--lblatl--}" />
<TextBox Grid.Row="0"
Grid.Column="1"
IsReadOnly="True"
Text="{Binding Path=atlOmschrijving,
Mode=OneWay}" />
<Label Grid.Row="1"
Grid.Column="0"
Content="{specialLocalization:Translate CtlBstGbr--lblInfo--}" />
<TextBox Grid.Row="1"
Grid.Column="1"
Height="75"
AcceptsReturn="True"
IsReadOnly="True"
Text="{Binding Path=atl.ArtInfo,
Mode=OneWay}"
TextWrapping="WrapWithOverflow"
VerticalScrollBarVisibility="Visible" />
</Grid>
</DataTemplate>
</DataGrid.RowDetailsTemplate>
</DataGrid>

最佳答案

确保第一列的宽度为“*”。宽度为“自动”,将使第一列的宽度初始化为内容的全宽。

关于c# - 如何使 WPF 数据网格列从一开始就自动调整大小,以便它们都可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48584444/

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