简体   繁体   中英

Can't add the converter while binding

I've implemented the interface IValueConverter as shown here and I put it for now in the same file as my code-behind to the XAML (two classes, both in the same namespace but implementing IValueConverter and inheriting Window ).

public class BamseGurka : IValueConverter
{
  public object Convert(object v, Type t, object p, CultureInfo c) { ... }
  public object ConvertBack(object v, Type t, object p, CultureInfo c) { ... }
}

In XAML I added a label, the visibility of which is bound to the outcome that need to be converted, as shown here - "how to use value converter in XAML" .

<Label Name="ErrorMessage" ...
       Visibility="{Binding Converter={StaticResource BamseConverter}}" >

Since I'll want to use a really custom converter and not one that's been forseen by Microsoft (no booleans, integers or such), I need to refer my implementation (as opposed to eg BoolToVisibilityConverter ). I hoped that the intellisense would give me good suggestion on the matter but I ended up in adding a resource to the parent element (or window, or application) like so.

<StackPanel.Resources>
  <ComboCellBindingConverter x:Key="BamseConverter" />
</StackPanel.Resources>

And this raises two issues - first of all why do I get (and what is) ComboCellBindingConverter . And secondly, what am I supposed to put there instead (since that symbol can't be resolved and I need to point to my implementation anyway)?

Being sneaky, I tried to circumvent as follows but that only led to the same error.

<StackPanel.Resources>
  <BamseGurka x:Key="BamseConverter" />
</StackPanel.Resources>

Using the intellisense, I arrived at a new attribute in the window tag and the following setup.

xmlns:blopp="clr-namespace:The.Actual.Namespace" ...

<StackPanel.Resources>
  <blopp:BamseGurka x:Key="BamseConverter" />
</StackPanel.Resources>

Now, it complains about BamseGurka not being in the mentioned namespace, which is a bold lie because it is! And just for fun, when I switched BamseGurka for the name of the original class (the other one in the same file, remember?), the error was gone.

So I'm kind of at a loss here. Haven't used custom converters like this before. Suggestions? Google gave me a lot to go on if DataContext is null but nother better when it comes to converters.

For example if you put your converter to separate file located in "Converters" folder in root folder, you would register it in dictionary like that:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:converters="clr-namespace:BamseGurka.Converters">

    <converters:BamseConverter x:Name="BamseConverter" />
</ResourceDictionary>

then you would just bind to it like you have done:

<Label Name="ErrorMessage" ...
       Visibility="{Binding Converter={StaticResource BamseConverter}}" >

of course, remember to set to what property you bind in Visibility, because for this moment it won't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM