具有MultiBinding的WPF TextBox.Text [英] WPF TextBox.Text with MultiBinding
问题描述
我在默认模板中具有带有文本框的自定义控件. 自定义控件具有以下2个依赖项属性(以及其他属性):
I've got Custom Control with a TextBox in the default Template. The Custom Control has these 2 dependency properties (among others):
SelectedValue,NullText(当未选择任何内容并提供值时显示在TextBox中的文本)
SelectedValue, NullText (text to appear in the TextBox when nothing is selected and the value is provided)
我想在SelectedValue
null为NullText
not null为
I'd like to set the TextBox.Text with the NullText value when the SelectedValue
null is and the NullText
not null is.
<TextBox.Text>
<MultiBinding Converter="{StaticResource myConverter}">
<Binding RelativeSource="TemplatedParent" Path="SelectedValue"/>
<Binding RelativeSource="TemplatedParent" Path="NullText"/>
</MultiBinding>
</TextBox.Text>
我有一个IMultiValueConverter:
I've got a IMultiValueConverter:
public class MyConverter : IMultiValueConverter
{}
有了这个XAML定义,我得到了'类型没有公共的TypeConverter类'异常
With this XAML definition I got 'type does not have a public TypeConverter class' Exception
请问您将如何解决?
推荐答案
我自己找到了解决方案: 问题出在RelativeSource上. 它应该是这样的:
I found the SOLUTION by myself: The problem was with the RelativeSource. This is how it should look like:
<TextBox.Text>
<MultiBinding Converter="{StaticResource myConverter}">
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="SelectedValue"/>
<Binding RelativeSource="{RelativeSource TemplatedParent}" Path="NullText"/>
</MultiBinding>
</TextBox.Text>
这篇关于具有MultiBinding的WPF TextBox.Text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!