2012-04-20 50 views
4

我显示弹出下面的代码:XAML找不到转换器类

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

我该怎么改变才能工作?

+0

尝试增加你的转换器类的对象到相关资源字典,然后使用'DynamicResource ' – nakiya 2012-04-20 02:51:33

回答

9

这听起来像你的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> 
+2

使用<转换器:BooleanOrConverter X:键= “BooleanOrConverter”/>。改变钥匙只是为了避免混淆。 – 2012-04-20 06:30:13