简体   繁体   English

将“从”和“到”属性绑定到控件高度

[英]Binding the “From” & “To” properties to a control height

Here is what I am attempting to do: I have a textblock (whose size isn't fixed) inside a grid. 这是我要尝试做的事情:我在网格内有一个文本块(其大小未固定)。 I want the grid's height to animate to the size of the textblock when the user presses a button, revealing all the content of the textblock. 我希望当用户按下按钮以显示文本块的所有内容时,网格的高度可以动画化为文本块的大小。

This is the xaml with the grid and textblock (simplified): 这是带有网格和文本块的xaml(简体):

<Grid x:Name="mygrid" ScrollViewer.VerticalScrollBarVisibility="Disabled" Height="38" Margin="10,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top">
    <TextBlock MaxWidth="400" x:Name="mytextblock" HorizontalAlignment="Left" Margin="10,0" TextWrapping="WrapWholeWords" VerticalAlignment="Top" FontSize="10.667" Foreground="#BFFFFFFF" Text="reallylongtextgoeshere"/>
</Grid>

This is my current code: 这是我当前的代码:

<Storyboard x:Name="ExtendDescription">
    <DoubleAnimation x:Name="DBED" Duration="00:00:00.6" Storyboard.TargetProperty="Height" EnableDependentAnimation="True" From="0" To="{Binding ElementName=mytextblock, Path=ActualHeight}" Storyboard.TargetName="mygrid">
       <DoubleAnimation.EasingFunction>
            <CircleEase EasingMode="EaseInOut"/>
       </DoubleAnimation.EasingFunction>
    </DoubleAnimation>
</Storyboard>

I have spent the last hour or so looking for solutions, but nothing I've come up with has worked. 我花了最后一个小时左右的时间来寻找解决方案,但是我没有想到的事情奏效了。 Any idea on how I can get this working? 关于如何使它起作用的任何想法吗? For the moment the animation has the rather odd behavior of animating the height down to 0, making the grid smaller, instead of larger :/ 目前,动画具有将高度动画化为0的相当奇怪的行为,使网格更小而不是更大:/

In the end, I fixed this by handling this via C#. 最后,我通过C#处理此问题。 When the Textblock SizeChanged event fires, the following code gets executed: 触发Textblock SizeChanged事件时,将执行以下代码:

myGrid.Visibility = Visibility.Visible;
DBED.To = DESCRIPTION.ActualHeight + 4;
DBED2.From = DESCRIPTION.ActualHeight + 4;

DBED and DBED2 being the DoubleAnimations. DBED和DBED2是DoubleAnimations。 The +4 is simply to have a little margin, so that the text doesn't get clipped off. +4只是留有一点空白,这样文本就不会被剪切掉。

I suspect handling this via xaml doesn't work because the ActualHeight property gets measured before anything is rendered, making it's value equals to 0. 我怀疑通过xaml处理此操作无效,因为在呈现任何内容之前已测量了ActualHeight属性,使其值等于0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM