XAML无法找到转换器类。

9

我正在使用以下代码显示弹出窗口:

<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>

这是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();
    }
}

它被放置在PopupTest.InfoPanels.Windows命名空间中

当我运行这个程序时,我收到以下异常:

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

我应该改变什么才能使这个工作?


尝试将您的转换器类对象添加到相关资源字典中,然后使用DynamicResource <resource_id> - nakiya
1个回答

14

看起来你的Multibinding不知道在哪里查找转换器。你是否将转换器定义为静态资源?你可以在控件的资源或包含的ResourceDictionary中指定转换器。添加对转换器命名空间的引用,然后为其定义一个ResourceKey。例如:

<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>

2
使用<converters:BooleanOrConverter x:Key="BooleanOrConverter"/>。更改键只是为了避免混淆。 - Shakti Prakash Singh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接