如何获得当前高度为 0 的 WPF UI 元素的所需高度? [英] How to get desired height of WPF UI element with current height of 0?

查看:17
本文介绍了如何获得当前高度为 0 的 WPF UI 元素的所需高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是动画化两个用户控件之间的大小变化.我试图实现这一目标的方式产生了一个主要问题.

My ultimate goal is to animate the change in size between two UserControls. The way that I have attempted to achieve this has produced one major problem.

我从一个包含一些基本文本和图标显示的 DataTemplate、一个高度设置为 0 的编辑"用户控件和一个编辑按钮开始.编辑 UserControl 位于 Height="Auto" 的 GridRow 中,因此它也以 0 的高度开始.该按钮具有由按钮单击触发的 DoubleAnimation,它将 UserControl 的高度从 0 设置为 300.这一切都有效正好.这是一个简化的代码示例.

I start with a DataTemplate that contains some basic text and icon display, an 'edit' UserControl with a height set to 0 and an edit button. The edit UserControl is in a GridRow with Height="Auto", so it also starts out with a height of 0. The button has a DoubleAnimation triggered by the button click that animates the height of the UserControl from 0 to 300. This all works just fine. Here is a simplified code example.

<DataTemplate x:Key="UserTemplate" DataType="{x:Type dataTypes:User}">
...
<controls:UserView Grid.Row="1" Grid.ColumnSpan="5" x:Name="EditRow" 
    DataContext="{Binding}" Height="0" />
<controls:UserEditor Grid.Row="2" Grid.ColumnSpan="5" x:Name="EditRow" 
    DataContext="{Binding}" Height="0" />
<Button Grid.Row="0" Grid.Column="4" Name="Edit" Style="{StaticResource ButtonStyle}" 
    ToolTip="Edit user" Click="Button_Click">
    <Image Source="/SolutionName;component/Images/Edit.png" Stretch="None" />
    <Button.Triggers>
        <EventTrigger RoutedEvent="Button.Click">
            <BeginStoryboard>
                <Storyboard>
                    <DoubleAnimation Name="EditRowHeightAnimation" 
    Storyboard.TargetName="EditRow" Storyboard.TargetProperty="Height" From="0" 
    To="300" Duration="00:00:0.5" />
                </Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Button.Triggers>
</Button>
...
</DataTemplate>

问题是编辑 UserControl 不是 300 像素高,我不知道它在设计时的高度.我尝试了以下方法,但它不起作用.

The problem is that the edit UserControl is not 300 pixels high and I won't know what height it will be at design time. I tried the following, but it doesn't work.

<DoubleAnimation Name="EditRowHeightAnimation" Storyboard.TargetName="EditRow"
    Storyboard.TargetProperty="Height" From="0" To="{Binding DesiredSize.Height, 
    ElementName=EditRow}" Duration="00:00:0.5" />

我还尝试从后面的代码对编辑 UserControl 调用 Measure() 和 UpdateLayout().我注释掉了按钮单击触发器和 xaml 动画,并从后面的代码中添加了一个......现在这种工作有效,但我总是得到相同的(错误的)DesiredSize.也就是说,UserControl 高度会被动画化,但只是高度错误.这是按钮单击处理程序代码.

I have also tried calling Measure() and UpdateLayout() on the edit UserControl from code behind. I commented out the button click trigger and xaml animation and added one from code behind instead... now this kind of worked, but I always got the same (wrong) DesiredSize. That is, the UserControl height would get animated, but just to the wrong height. Here is the button click handler code.

private void Button_Click(object sender, RoutedEventArgs e)
{
    User currentUser = (User)CollectionViewSource.GetDefaultView(Users).CurrentItem;
    ListBoxItem listBoxItem = (ListBoxItem)
        (UsersListBox.ItemContainerGenerator.ContainerFromItem(currentUser));
    DataTemplate itemDataTemplate = FindResource("UserTemplate") as DataTemplate;
    ContentPresenter contentPresenter = listBoxItem.GetInternal<ContentPresenter>();
    if (itemDataTemplate != null && contentPresenter != null)
    {
        UserEditor userEditor = (UserEditor)itemDataTemplate.FindName("EditRow", 
            contentPresenter);
        userEditor.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
        userEditor.UpdateLayout();
        userEditor.BeginAnimation(HeightProperty, new DoubleAnimation(0, 
            userEditor.DesiredSize.Height, new Duration(new TimeSpan(0, 0, 1)), 
            FillBehavior.HoldEnd), HandoffBehavior.Compose);
    }
}

所以我的问题是,如果 UserControl 的容器对它没有大小限制,当它的当前高度为 0 时,我如何获得 UserControl 的大小?

So my question is how can I get the size that a UserControl would be if its container placed no size restriction upon it, when its current height is 0?

如果有更好的方法来实现我的最终目标,我也很高兴.非常感谢.

I'd also be happy to hear if there is a better way to achieve my ultimate goal. Many thanks in advance.

推荐答案

你可以用 ClipToBound="True" 把你想要大小的内容放到一个Canvas中.然后,您可以操纵 Canvas 的大小,而 Canvas 内的内容大小将始终是其所需的完整大小.

You can put the content you want the size of into a Canvas with ClipToBound="True". Then you can manipulate the size of the Canvas and yet the size of the content inside the Canvas will always be its full desired size.

这篇关于如何获得当前高度为 0 的 WPF UI 元素的所需高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆