Search code examples
c#wpfxamlivalueconverter

XAML can't find the converter class


I'm displaying a popup with the following code:

<Popup PlacementTarget="{Binding ElementName=categoryTagEditorControl}"
       Placement="Bottom">
    <Popup.IsOpen>
        <MultiBinding Mode="OneWay" Converter="{StaticResource BooleanOrConverter}">
            <Binding Mode="OneWay" ElementName="categoryTagEditorControl" Path="IsMouseOver"/>
            <Binding RelativeSource="{RelativeSource Self}" Path="IsMouseOver" />
        </MultiBinding>
    </Popup.IsOpen>
    <StackPanel>
        <TextBox Text="Some Text.."/>
        <DatePicker/>
    </StackPanel>
</Popup>

Here's the code of BooleanOrConverter:

public class BooleanOrConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        foreach (object booleanValue in values)
        {
            if (booleanValue is bool == false)
            {
                throw new ApplicationException("BooleanOrConverter only accepts boolean as datatype");
            }
            if ((bool)booleanValue == true)
            {
                return true;
            }
        }
        return false;
    }
    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

and its placed into PopupTest.InfoPanels.Windows namespace

when I run this, I'm getting following exception:

Cannot find resource named 'BooleanOrConverter'. Resource names are case sensitive.

What should I change for this to work?


Solution

  • It sounds like your Multibinding doesn't know where to look for the converter. Have you defined the converter as a staticresource? You can either specify the converter in the control's resources or in the included ResourceDictionary. Add a reference to the converter's namespace and then define a ResourceKey for it. Something like:

    <UserControl 
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:converters="clr-namespace:MyConverters">
    
         <UserControl.Resources>
              <converters:BooleanOrConverter x:Key="BoolOrConverter"/>
         </UserControl.Resources>
    
        ... // use converter as you were before
    
     </UserControl>