0

我在这里有一个文本框

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

它的内容依赖于其他 ComboBox

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

如果选择Sample1Sample3,则 TextBox 应绑定到MinStepDiff

如果选中,则Sample2TextBox 应绑定到MinTolerance

它们都是对象的属性。

我该怎么做?

4

1 回答 1

1

你可以使用一个DataTrigger. 为此,您必须创建一个样式并ComboBox命名(此处为“cb”)。因为它更容易我绑定SelectedIndex而不是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>
于 2012-05-20T01:46:31.513 回答