WPF TextBox.Text with MultiBinding

2019-04-23 16:01发布

问题:

I've got Custom Control with a TextBox in the default Template. The Custom Control has these 2 dependency properties (among others):

SelectedValue, NullText (text to appear in the TextBox when nothing is selected and the value is provided)

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>

I've got a IMultiValueConverter:

public class MyConverter : IMultiValueConverter
{}

With this XAML definition I got 'type does not have a public TypeConverter class' Exception

How would you solve it, please?

回答1:

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>