Byzod
Byzod

Reputation: 486

How to change the Path of existed Binding?

I got a TextBox here

<TextBox ...>
    <TextBox.Text>
        <Binding Path="MinStepDiff" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <local:ImpellerArgsRule IsCanBeZero ="false"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

It's Content is rely on other ComboBox

<ComboBox ...>
    <ComboBoxItem Content="Sample1"/>
    <ComboBoxItem Content="Sample2"/>
    <ComboBoxItem Content="Sample3"/>
</ComboBox>

If Sample1 or Sample3 is chosen, the TextBox should be bind to MinStepDiff

If Sample2 is chosen, the TextBox should be bind to MinTolerance then

Both of them are properties of an object.

How can I do it?

Upvotes: 0

Views: 145

Answers (1)

LPL
LPL

Reputation: 17063

You could use a DataTrigger. For this you have to create a style and give your ComboBox a name (here 'cb'). Because it's easier I bound to SelectedIndex instead of SelectedItem.

<TextBox>
    <TextBox.Style>                
        <Style TargetType="TextBox">
            <Setter Property="Text">
                <Setter.Value>
                    <Binding Path="MinStepDiff" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <local:ImpellerArgsRule IsCanBeZero="false" />
                        </Binding.ValidationRules>
                    </Binding>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <DataTrigger Binding="{Binding SelectedIndex, ElementName=cb}" Value="1">
                    <Setter Property="Text">
                        <Setter.Value>
                            <Binding Path="MinTolerance" UpdateSourceTrigger="PropertyChanged" />
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBox.Style>
</TextBox>

Upvotes: 1

Related Questions