C# 使用Dependency属性根据WPF中另一个TextBock的值更新TextBlock的值
在我的XAML中,我有以下两个文本块C# 使用Dependency属性根据WPF中另一个TextBock的值更新TextBlock的值,c#,.net,wpf,C#,.net,Wpf,在我的XAML中,我有以下两个文本块 <TextBlock Name="tbGeneratedSignature" TextWrapping="Wrap" Margin="2,2,0,0" Height="auto" Width="390" Foreground="Black" TextDecorations="None" VerticalAlignment="Top" Focusable="False"/> <TextBlock Name="tbSignatureTe
<TextBlock Name="tbGeneratedSignature" TextWrapping="Wrap" Margin="2,2,0,0" Height="auto" Width="390" Foreground="Black" TextDecorations="None" VerticalAlignment="Top" Focusable="False"/>
<TextBlock Name="tbSignatureText" TextWrapping="Wrap" Margin="5" Height="auto" Width="440" Foreground="Black" />
基于tbGeneratedSignature.Text我想只使用XAML而不是使用C#为tbSignatureText.Text指定相同的值。您考虑过绑定吗
<TextBlock Name="tbSignatureText"
Text="{Binding ElementName=tbGeneratedSignature, Path=Text, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Margin="5"
Height="auto"
Width="440"
Foreground="Black" />
感谢您提供了简单高效的方法,即使我没有编写UpdateSourceTrigger=PropertyChangedYea,我也不确定TextBlock
的默认绑定是UpdateSourceTrigger=PropertyChanged
还是LostFocus
。当您绑定到TextBox
的Text
属性时,它默认为UpdateSourceTrigger=LostFocus
,因此您必须将其更改为PropertyChanged
<TextBlock Name="tbGeneratedSignature" TextWrapping="Wrap" Margin="2,2,0,0" Height="auto" Width="390" Foreground="Black" TextDecorations="None" VerticalAlignment="Top" Focusable="False"/>
<TextBlock Name="tbSignatureText" Text="{Binding ElementName=tbGeneratedSignature, Path=Text}" TextWrapping="Wrap" Margin="5" Height="auto" Width="440" Foreground="Black" />