具有多重绑定的 WPF TextBox.Text
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2185168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF TextBox.Text with MultiBinding
提问by PaN1C_Showt1Me
I've got Custom Control with a TextBox in the default Template. The Custom Control has these 2 dependency properties (among others):
我在默认模板中有一个带有文本框的自定义控件。自定义控件具有以下 2 个依赖项属性(以及其他属性):
SelectedValue, NullText (text to appear in the TextBox when nothing is selected and the value is provided)
SelectedValue、NullText(当未选择任何内容并提供值时,将出现在 TextBox 中的文本)
I'd like to set the TextBox.Text with the NullTextvalue when the SelectedValue
null is and the NullText
not null is.
我想在null 和not null时使用 NullText值设置 TextBox.Text。SelectedValue
NullText
<TextBox.Text>
<MultiBinding Converter="{StaticResource myConverter}">
<Binding RelativeSource="TemplatedParent" Path="SelectedValue"/>
<Binding RelativeSource="TemplatedParent" Path="NullText"/>
</MultiBinding>
</TextBox.Text>
I've got a IMultiValueConverter:
我有一个 IMultiValueConverter:
public class MyConverter : IMultiValueConverter
{}
With this XAML definition I got 'type does not have a public TypeConverter class' Exception
有了这个 XAML 定义,我得到了“类型没有公共 TypeConverter 类”异常
How would you solve it, please?
请问你会怎么解决?
回答by PaN1C_Showt1Me
I found the SOLUTIONby 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>