猿问

Datagrid高度展开窗口

现在我在一个窗口中有一个数据网格,在某处单击按钮后会填满,每当数据网格填满时,它就会超过窗口的当前大小并进一步扩展窗口。确保数据网格目前只能扩展到窗口的当前大小的方法是什么。


我要求窗口之后能够手动调整大小,因此无法设置最大高度。


    <Grid>

    <Grid.RowDefinitions>

        <RowDefinition Height="*"/>

        <RowDefinition Height="40" MinHeight="40"/>

    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>

        <ColumnDefinition Width="Auto"/>

        <ColumnDefinition Width="*"/>

        <ColumnDefinition Width="Auto" MinWidth="250"/>

    </Grid.ColumnDefinitions>


    <GroupBox Grid.Column="0 Margin="10,0,5,0">

        <Grid MaxHeight="{Binding ActualHeight, ElementName= AddressTable}">

            <Grid.RowDefinitions>

                <RowDefinition/>

            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>

                <ColumnDefinition Width="Auto"/>

                <ColumnDefinition Width="Auto"/>

            </Grid.ColumnDefinitions> 

                <DataGrid Name="AddrTable" VerticalScrollBarVisibility="Auto"

                        Grid.Row="0"


红颜莎娜
浏览 130回答 1
1回答

心有法竹

我不确定这是否是您想要的,但是Height使用 Window 使 MaxHeight 动态化的一种方法是使用RelativeSource. 让我解释:在您的Grid中,您将 设置MaxHeight为ActualHeight窗口的。为此,您只需要绑定MaxHeigth = "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=ActualHeight}"现在,这不是一个很好的解决方案,因为窗口内有边距、填充和更多项目。所以你想要的是Grid使ActualHeight. 因此,您需要一个Converter。我已经为您制作了这个,它会返回一定百分比的价值。public class SizePercentageConverter : IValueConverter{&nbsp; &nbsp; public object Convert(object value, Type targetType, object parameter, CultureInfo culture)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (value == null)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0:&nbsp; &nbsp; &nbsp; &nbsp; if (parameter == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (double)value;&nbsp; &nbsp; &nbsp; &nbsp; var split = parameter.ToString().Split('.');&nbsp; &nbsp; &nbsp; &nbsp; var parameterDouble = double.Parse(split[0]) + double.Parse(split[1]) / Math.Pow(10, split[1].Length);&nbsp; &nbsp; &nbsp; &nbsp; return (double)value * parameterDouble;&nbsp; &nbsp; }&nbsp; &nbsp; public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // Don't need to implement this&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }在ResourceDictionary中定义它之后<converters:SizePercentageConverter x:Key="PercentageConverter" />你可以在你的MaxHeight. 例如,如果你希望它是的70%,ActualHeight你只需要写MaxHeight = "{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}, Path=ActualHeight, Converter={StaticResource PercentageConverter}, ConverterParameter=0.7}"否则,如果您还可以创建一个减去一个值的转换器。例如,一旦绑定它就返回ActualHeight- parameter。我希望这会有所帮助,并让我知道进展情况。
随时随地看视频慕课网APP
我要回答