WPF:将我的TextBox与我的Property一起传递到我的转换器中

时间:2018-02-22 13:54:02

标签: wpf textbox

所以我有 const w = mount(<Component />).render(); expect(w.hasClass("root")).toBeTruthy(); expect( w .children() .first() .hasClass("Bar") ).toBeTruthy();

Converter

我的绑定对象具有此属性(实现public class ComboboxSelectedIndexToTextBoxBackgroundColor : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { int selectedIndex = (int)value; if (selectedIndex == 0) return "Red"; else return "Green"; } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } ):

INotifyPropertyChanged

我的public int ComboboxSelectedIndex { get { return _comboboxSelectedIndex; } set { _comboboxSelectedIndex = value; OnPropertyChanged(); } }

TextBox

因此,如果我想使用<TextBox Controls:TextBoxHelper.ClearTextButton="False" Background="{Binding ComboboxSelectedIndex, Converter={StaticResource ComboboxSelectedIndexToTextBoxBackgroundColor}}" Margin="0,0,0,0"> 并且在我的MultiBindingConverter属性中,我希望slao发送我的ComboboxSelectedIndex - 是否可能? 我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

Name="txt"属性添加到TextBox并在绑定中使用ElementName。 TextBox将成为绑定源,没有属性Path它将被发送到转换器本身,而不是属性值。

<MultiBinding Converter="{StaticResource MvCvt}" Mode="OneWay">
    <Binding Path="ComboboxSelectedIndex"/>
    <Binding ElementName="txt"/>
</MultiBinding>

元素也可以使用{RelativeSource Self}

将自身发送到绑定
<MultiBinding Converter="{StaticResource MvCvt}" Mode="OneWay">
    <Binding Path="ComboboxSelectedIndex"/>
    <Binding RelativeSource="{RelativeSource Self}"/>
</MultiBinding>

“McCvt”是这里的一些IMultiValueConverter实施