0
votes

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 ?

1

1 Answers

1
votes

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.