C# UIElement中的值更改后执行方法
是否可以订阅WPF中特定UIElement的属性 我想在Heightvalue更改后立即设置C# UIElement中的值更改后执行方法,c#,wpf,animation,uielement,C#,Wpf,Animation,Uielement,是否可以订阅WPF中特定UIElement的属性 我想在Heightvalue更改后立即设置UIElement的动画,并将新高度添加到列表中,但我不知道如何订阅HeightProperty 样本代码: 大概是这样的: MainWindow.xaml: <Window x:Class="BibVisualization.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x
UIElement
的动画,并将新高度添加到列表中,但我不知道如何订阅HeightProperty
样本代码:
大概是这样的:
MainWindow.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>
我认为您可以使用FrameworkElement类的-Event来做您想要做的事情。所有ui元素
Button或Textblock
都派生自该类,因此提供事件
传递到注册的方法包含高度或宽度已更改的信息,并提供新值。您可以使用
DependencyPropertyDescriptor
为属性添加ValueChangedHandler
:
DependencyPropertyDescriptor descriptor=DependencyPropertyDescriptor.FromProperty(UIElement.HeightProperty,typeof(UIElement));
descriptor.AddValueChanged(myUIElementToWatch, new EventHandler(OnHeightOfUiElementChanged));
如果我理解正确,您是否希望“绑定”到实际高度 看看这个链接()——它描述了如何使用附加的属性来完成它 再看看我前几天的回答,它基本上描述了非常相似的问题。
(使用此处的链接下载支持代码-您可以通过
Style
或文章中描述的方式绑定到-这都是类似的事情)
您需要的是使用本文中描述的方法绑定到ActiveHeight
,该方法更改视图模型的MyHeight
属性-处理它的set
以在活动高度更改时获取。如果有任何问题,请告诉我
希望有帮助。听起来很有希望,但如果在初始化窗口后执行此操作,则只会触发一次,无论如何,谢谢!
DependencyPropertyDescriptor descriptor=DependencyPropertyDescriptor.FromProperty(UIElement.HeightProperty,typeof(UIElement));
descriptor.AddValueChanged(myUIElementToWatch, new EventHandler(OnHeightOfUiElementChanged));