我有一个奇怪的问题,你们可以快速解决: - )
在页面中有一个带有一些列的WPF DataGrid,我希望在用户调整窗口大小时动态调整列宽,这是拉伸效果。
当我设置这样的列时:
<DataGridTextColumn Header="Column A" Binding="{Binding ColumnA}" Width="*"/>
运行我的应用程序,窗口被冻结,列不断扩展,就像动画一样...... 那么如何保持列的位置并仅在用户调整窗口大小时调整大小。
对于凌乱的WPF及其实验代码感到抱歉。
<Page x:Class="SharepointUploader.Pages.FieldMapping"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:SharepointUploader.Pages"
mc:Ignorable="d"
Title="FieldMapping" Background="White" HorizontalAlignment="Stretch" >
<Page.Resources>
<local:DGRows x:Key="dgRows" />
<CollectionViewSource x:Key="cvsRows" Source="{StaticResource dgRows}">
<CollectionViewSource.GroupDescriptions>
<PropertyGroupDescription PropertyName="NotesForm"/>
</CollectionViewSource.GroupDescriptions>
</CollectionViewSource>
<Style TargetType="DataGrid">
<Setter Property="VerticalGridLinesBrush" Value="Transparent"/>
<Setter Property="HorizontalGridLinesBrush" Value="Transparent"/>
<Setter Property="ItemsSource" Value="{Binding Source={StaticResource cvsRows}}"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="Height" Value="700"/>
<Setter Property="AutoGenerateColumns" Value="False"/>
<Setter Property="CanUserAddRows" Value="False"/>
<Setter Property="CanUserResizeRows" Value="False"/>
<Setter Property="SelectionMode" Value="Single"/>
<Setter Property="AlternatingRowBackground" Value="LightBlue"/>
<Setter Property="AlternationCount" Value="2"/>
<Setter Property="HeadersVisibility" Value="Column"/>
<Setter Property="Margin" Value="5" />
</Style>
<Style TargetType="DataGridRow">
<Setter Property="Foreground" Value="Black"/>
<Setter Property="Height" Value="40" />
</Style>
<Style TargetType="DataGridCell">
<Setter Property="TextBlock.TextAlignment" Value="Left" />
<Setter Property="TextBlock.VerticalAlignment" Value="Center"/>
<!--No row highlighting on selection-->
<Style.Triggers>
<Trigger Property="DataGridCell.IsSelected" Value="True">
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background" Value="Transparent"/>
</Trigger>
</Style.Triggers>
</Style>
</Page.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<DataGrid x:Name="dgFields" Grid.Row="0" Width="{Binding ElementName=Page, Path=ActualWidth}">
<DataGrid.GroupStyle>
<!-- Style for groups at top level. -->
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type GroupItem}">
<Expander IsExpanded="True" Background="White" Foreground="White" Width="Auto">
<Expander.Header>
<DockPanel Background="LightGray">
<TextBlock Foreground="Black" Text="{Binding Path=Name, StringFormat=Form {0}}"/>
</DockPanel>
</Expander.Header>
<Expander.Content>
<ItemsPresenter/>
</Expander.Content>
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</GroupStyle.ContainerStyle>
</GroupStyle>
</DataGrid.GroupStyle>
<DataGrid.Columns>
<DataGridCheckBoxColumn Header="" Binding="{Binding Selected, Mode=TwoWay}" Width="10" CanUserResize="False" CanUserReorder="False"/>
<DataGridTextColumn Header="Column A" Binding="{Binding ColumnA}" Width="*"/>
<DataGridTextColumn Header="Column B" Binding="{Binding ColumnB}" Width="Auto" />
<DataGridTextColumn Header="Column C" Binding="{Binding ColumnC}" Width="Auto " />
<DataGridTextColumn Header="Column D" Binding="{Binding ColumnD}" Width="Auto" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Page>
答案 0 :(得分:0)
由于您将数据网格放置在没有定义列宽的网格中,因此实际上为DataGrid宽度提供了无限空间,除了将列定义添加到容器网格,然后将列设置为& #34; *&#34;或&#34;自动&#34;
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition MaxWidth="Your_Max_Limit_To_Width"/>
</Grid.ColumnDefinitions>
</Grid>
此外,请确保您需要3列作为自动,一列需要*,自动告诉列从可用中获取所需数量,因此如果第一列想要填满所有空间,您将看不到其他列,更好的方法是给所有人提供一个*甚至更好地完全删除宽度定义。
答案 1 :(得分:0)
感谢您的所有答案。
我已经转储了Page方法,并为流程的每个步骤配备了单独的窗口。 代码在Window中按预期工作,因此我怀疑问题出现在Page和窗口内的框架中。