在WPF中,有没有办法绑定兄弟属性? [英] In WPF, is there a way to bind to sibling properties?
本文介绍了在WPF中,有没有办法绑定兄弟属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一系列 TextBlock
和 TextBox
控件。有没有办法将 Style
应用于 TextBlock
,以便他们可以立即将数据绑定到控件?
I have a series of TextBlock
and TextBox
controls. Is there a way to apply a Style
to the TextBlock
s such that they can databind to the control immediately after them?
我想要这样做:
<Resources..>
<Style x:Key="BindToFollowingTextBoxSibling">
<Setter Property="TextBlock.Text" Value="{Binding RelativeSource={RelativeSource FollowingSibling}, Path=Text, Converter={StaticResource MyConverter}}" />
<Setter Property="TextBlock.Background" Value="{Binding RelativeSource={RelativeSource FollowingSibling}, Path=Text, Converter={StaticResource TextToBrushConverter}}" />
... More properties and converters.
</Style>
</Resources>
...
<TextBlock Style="{StaticResource BindToFollowingTextBoxSibling}"/>
<TextBox/>
<TextBlock Style="{StaticResource BindToFollowingTextBoxSibling}"/>
<TextBox/>
<TextBlock Style="{StaticResource BindToPreviousTextBoxSibling}"/>
这样的事情甚至可能吗?
Is something like this even possible?
推荐答案
我认为在这种情况下最好的事情是由ElementName绑定:
I think the best thing to do in this case is bind by ElementName:
<TextBlock Text="{Binding ElementName=textBox1, Path=Text}" />
<TextBox x:Name="textBox1">this is the textBox's 1 text</TextBox>
<TextBlock Text="{Binding ElementName=textBox2, Path=Text}" />
<TextBox x:Name="textBox2">this is the textBox's 2 text</TextBox>
它将实现类似的东西。这是否适合你?
It will achieve something similar. Does this work for you?
这篇关于在WPF中,有没有办法绑定兄弟属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文