gpt4 book ai didi

c# - UIElement 的值更改后执行方法

转载 作者:太空宇宙 更新时间:2023-11-03 16:17:24 24 4
gpt4 key购买 nike

是否可以在 WPF 中订阅特定 UIElement 的属性?

我想在 Heightvalue 更改后立即为 UIElement 设置动画并将新高度添加到列表中,但我不知道如何订阅 HeightProperty?

示例代码:

像这样:

主窗口.xaml:

<Window x:Class="BibVisualization.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>

<Border Background="Red" Width="30" Grid.Row="0" x:Name="myBorder">
<TextBlock Text="Really really long text with wrapping, but the wrapping changes based on border's width"
Width="{Binding ElementName=myBorder, Path=Width}"
TextWrapping="Wrap" />
</Border>
<Button Grid.Row="1" Height="10"
Content="Make border bigger" Click="OnButtonClick" />
</Grid>
</Window>

主窗口.xaml.cs

private void OnButtonClick(Object sender, RoutedEventArgs e)
{
myBorder.Width += 10;
//Bind to textblock's actualheight and execute OnHeightChange?
}

private int accumulatedChange;

private void OnHeightChange(Object sender, SomeEventArgs? e)
{
accumulatedChange -= e.OldValue (if possible);
accumulatedChange += e.NewValue;
}

最佳答案

我想你可以使用 SizeChanged - FrameworkElement 类的事件来做你想做的事。所有 UIElement(例如 ButtonTextblock)都派生自该类,因此提供事件。

SizeChangedEventArgs传递给注册方法的信息包含高度或宽度是否已更改并提供新值的信息。

关于c# - UIElement 的值更改后执行方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15392435/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com