如何在wpf中限制网格行的高度

时间:2010-01-10 17:23:01

标签: wpf grid user-controls

我正在开发WPF中的Outlook加载项。 Outlook加载项是一个UserControl。我的简化XAML代码看起来是:

<UserControl>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="150"/>
                <RowDefinition Height="*"/>
                <RowDefinition Height="20"/>
            </Grid.RowDefinitions>
            <TextBlock Grid.Row="0">Header</TextBlock>
            <ListBox Grid.Row="1"></ListBox>
            <Button Grid.Row="2"></Button>
        </Grid>
    </UserControl>

列表框项是动态加载的。在我在这里创建的这个类http://www.codeproject.com/KB/WPF/GridLengthAnimation.aspx的帮助下,当按下按钮时,第3行的高度设置为0.4 *。

问题在于,当我在列表框中有更多项目时,第二行会扩展,第三行会消失。解决方案可能是我将第二行的MaxHeight设置为100%height-170,但我不知道UserControl的可用高度。

有什么想法吗?

2 个答案:

答案 0 :(得分:0)

而不是指定Height尝试为第3行指定MinHeight,这样它就不会缩小到超过一定的大小。

答案 1 :(得分:0)

问题出在这里(在我使用过的图书馆)

 public override object GetCurrentValue(object defaultOriginValue,
        object defaultDestinationValue, AnimationClock animationClock)
    {
        double fromVal = ((GridLength)GetValue(GridLengthAnimation.FromProperty)).Value;
        double toVal = ((GridLength)GetValue(GridLengthAnimation.ToProperty)).Value;

        if (fromVal > toVal)
        {
            return new GridLength((1 - animationClock.CurrentProgress.Value) * (fromVal - toVal) + toVal, GridUnitType.Pixel);
        }
        else
            return new GridLength(animationClock.CurrentProgress.Value * (toVal - fromVal) + fromVal, GridUnitType.Pixel);
    }

我已将GridUnitType.Star替换为GridUnitType.Pixel。现在它的工作非常完美