【问题标题】:How to change the Path of existed Binding?如何更改已存在的 Binding 的 Path?
【发布时间】:2012-05-19 10:39:03
【问题描述】:

我这里有一个文本框

<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

如果选择Sample2,那么TextBox应该绑定到MinTolerance

它们都是对象的属性。

我该怎么做?

【问题讨论】:

标签: c# wpf binding


【解决方案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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-23
    • 2016-12-18
    • 2017-10-16
    • 1970-01-01
    相关资源
    最近更新 更多