在WPF中,有没有办法绑定兄弟属性?

编程入门 行业动态 更新时间:2024-10-28 06:36:13
本文介绍了在WPF中,有没有办法绑定兄弟属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一系列 TextBlock 和 TextBox 控件。有没有办法将 Style 应用于 TextBlock ,以便他们可以立即将数据绑定到控件?

I have a series of TextBlock and TextBox controls. Is there a way to apply a Style to the TextBlocks 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中,有没有办法绑定兄弟属性?

本文发布于:2023-11-03 14:26:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1555361.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:没有办法   绑定   属性   兄弟   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!