WPF xamdatagrid高度+内容的窗口大小

本文关键字:窗口大小 xamdatagrid 高度 WPF | 更新日期: 2023-09-27 18:25:25

我有一个SizeToContent="WidthAndHeight"的窗口,启动位置设置为手动。此窗口包含一个包含两行的网格:第一行高度为"1*",第二行高度为自动高度。第一行包含xamdatagrid(基础),第二行包含OkCancel按钮。

xamdatagrid初始化后,窗口被垂直拉伸到可见区域之外。

我不想将最大高度绑定到基于系统参数和其他常量的计算中。

参见:窗口截图

代码:

<Window x:Class="CopyRenameLineItems.CopyAndRename"
         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:igDP="http://infragistics.com/DataPresenter"
         x:Name="CopyWindow" Title="Copy/Rename Line Item"
         mc:Ignorable="d" 
         d:DesignHeight="300" d:DesignWidth="500"
         d:DataContext="{d:DesignInstance d:Type=copyRenameLineItems:CopyAndRenameViewModel}" SizeToContent="WidthAndHeight" >
<Window.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Assets.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Window.Resources>
<Grid Margin="7" Name="MainGrid">
    <Grid.RowDefinitions>
        <RowDefinition Height="1*"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid Grid.Row="0">
        <igDP:XamDataGrid Name="CopyRenameGrid" Margin="5" GroupByAreaLocation="None" DataSource="{Binding Rows,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"
                            AutoFit="True" RecordContainerGenerationMode="PreLoad" >
            <igDP:XamDataGrid.FieldSettings>
                <igDP:FieldSettings
                    CellValuePresenterStyle="{StaticResource CellsBorder}"
                    AutoSizeOptions="All"
                AllowEdit="True"   
                AllowRecordFiltering="True"
                FilterLabelIconDropDownType ="MultiSelectExcelStyle" 
                CellClickAction="EnterEditModeIfAllowed"/>
            </igDP:XamDataGrid.FieldSettings>
            <igDP:XamDataGrid.FieldLayoutSettings>
                <igDP:FieldLayoutSettings 
                    AllowClipboardOperations="All"                                
                    AutoGenerateFields="False"
                    AutoFitMode="Never"
                    FilterRecordLocation="OnTop"                                
                    FilterUIType="LabelIcons"  />
            </igDP:XamDataGrid.FieldLayoutSettings>
            <igDP:XamDataGrid.FieldLayouts>
                <igDP:FieldLayout>
                    <igDP:FieldLayout.Fields>
                        <igDP:Field Name="Copy" >
                            <igDP:Field.Settings>
                                <igDP:FieldSettings Width="Auto" LabelPresenterStyle="{StaticResource CopyHeader}" />
                            </igDP:Field.Settings>
                        </igDP:Field>
                        <igDP:Field Name="Name" Label="Name"">
                            <igDP:Field.Settings>
                                <igDP:FieldSettings AllowEdit="False" Width="Auto" />
                            </igDP:Field.Settings>
                        </igDP:Field>
                        <igDP:Field Name="Description" Label="Description">
                            <igDP:Field.Settings>
                                <igDP:FieldSettings AllowEdit="False" Width="Auto"/>
                            </igDP:Field.Settings>
                        </igDP:Field>
                        <igDP:Field Name="NewName" Label="New name" Width="200">
                        </igDP:Field>
                    </igDP:FieldLayout.Fields>
                </igDP:FieldLayout>
            </igDP:XamDataGrid.FieldLayouts>
        </igDP:XamDataGrid>
    </Grid>
    <Grid Grid.Row="1" Margin="0,7,0,0" VerticalAlignment="Bottom">
        <Grid.ColumnDefinitions >
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid Grid.Column="0">
            <Button Height="25" Width="45" HorizontalAlignment="Right" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" 
                    Content="OK" Command="{Binding CopyRenameWindowOkButtonClick}" CommandParameter="{Binding ElementName=CopyWindow}" Margin="0,0,15,0"/>
        </Grid>
        <Grid Grid.Column ="1">
            <Button Height="25" Width="45" HorizontalAlignment="Right" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="Cancel" Command="{Binding  CloseWindowCancelButtonClick}"
                CommandParameter="{Binding ElementName=CopyWindow}"/>
        </Grid>
    </Grid>

</Grid>
</Window>

谢谢,

Zehavit

WPF xamdatagrid高度+内容的窗口大小

首先,对于SizeToContent="WidthAndHeight",值1*没有任何意义。如果将"1*"更改为"自动",则将具有相同的行为。您应该删除SizeToContent="WidthAndHeight"并执行以下操作:

<Grid >
    <Grid.RowDefinitions>
        <RowDefinition Height="3*"/>
        <RowDefinition Height="1*"/>
    </Grid.RowDefinitions>
    <Grid>
        your list ..
    </Grid>
    <Grid Grid.Row="1">
        you buttons ..
    </Grid>
</Grid>

您的第一行(列表)将是窗口大小的(3/(3+1))%

第二个(1/(3+1))%。

不要使用

SizeToContent="宽度和高度"

因为它将把CCD_ 2的CCD_。而且你的XamDataGrid有很多records,所以Window会长得很大。而只是删除该属性,则content将根据Window进行调整。

先给你的Window一些Height(就像尝试size0,WindowHeightSystem.Windows.SystemParameters.PrimaryScreenHeight),然后你可以使用SizeToContentWindow不会无限期增长。此外,Grid行的高度是不相关的,因为您没有给任何controlGrid任何固定的Height

这就解决了问题。