c# - XAML 找不到转换器类

标签 c# wpf xaml ivalueconverter

我正在显示一个带有以下代码的弹出窗口:

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

我应该更改什么才能使其正常工作?

最佳答案

听起来您的 Multibinding 不知道去哪里寻找转换器。您是否将转换器定义为 staticresource ?您可以在控件的资源或包含的 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>

关于c# - XAML 找不到转换器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10239517/

相关文章:

.net - 如何将此 StackPanel 用作资源

c# - 模拟 MessageBox.Show()

xaml - 是否可以在 VisualState 中缩放控件

wpf - 如何与 StringFormat 绑定(bind)

c# - Web 服务 Windows Phone 7 (405) 方法不允许

c# - C# 数组中的线程安全

c# - 如何检查一个值在数组中出现了多少次?

c# - 如何应用TextBox控件模板?

.net - UWP(通用 Windows 应用程序)中的 LoopingSelector?

C# 委托(delegate) : literal constructor