curiousity
curiousity

Reputation: 4741

WPF Binding One TextBox to two Properties

Perhaps a simple question, but... So, I have bound a text box to a property in ViewModel:

    <TextBox x:Name="ololo"
 Text="{Binding VM.OloloText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">

And there is a TextBlock near , that could change its Text property value due to some trigger logic that I have implemented beforehand:

<Style TargetType="TextBlock">
     <Style.Triggers>
          <DataTrigger Binding="{Binding ElementName=typeGallery, Path=SelectedValue}"
              Value="FirstType">
        <Setter Property="Text" Value="AAA" />
          </DataTrigger>
           <DataTrigger Binding="{Binding ElementName=typeGallery, Path=SelectedValue}"
                Value="Second Type">
            <Setter Property="Text" Value="BBB" />
           </DataTrigger> 
                                       ...

So, this TextBlock has values AAA or BBB . All this is working like a charm. The question is how to bind the ololo TextBox to one property, say VM.OloloText if there is AAA value in TextBlock and to other property (VM.ololoText2) if TextBlock value is BBB ?

Upvotes: 0

Views: 1589

Answers (1)

Sheridan
Sheridan

Reputation: 69959

You could use a couple of DataTriggers to do that for you:

<TextBox x:Name="ololo">
    <TextBox.Style>
        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Text" Value="{Binding VM.OloloText}" />
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, ElementName=YourTextBlock}"
                    Value="AAA">
                    <Setter Property="Text" Value="{Binding OneProperty}" />
                </DataTrigger>
                <DataTrigger Binding="{Binding Text, ElementName=YourTextBlock}" 
                    Value="BBB">
                    <Setter Property="Text" Value="{Binding AnotherProperty}" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

Note that the Binding to the VM.OloloText property will only work if the TextBlock does not have AAA or BBB in it.

Upvotes: 1

Related Questions