views:

446

answers:

1

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?

I'd like to be able to do something like this:

<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?

+3  A: 

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?

Carlo
That's kind of what I'm likely going to do. The problem is that there are several properties and converters that I'm looking at binding, so it gets to be a lot of copy and past to do this for every property. I'm thinking I might bind by ElementName to the TextBlock's Tag and then use RelativeSource Self to get the properties that I need.
Eclipse
Did this ever work for you?
Carlo
I ended up just creating a user-control that handled the cases that I needed. It was just easier.
Eclipse
Haha cool man, my solution wasn't an optimal approach to your problem, glad you solved it =)
Carlo