C# 项目与System.Windows for iValueConverter之间的差异
有几个问题与此相关,但我的问题与我以前从未见过的另一个错误有关 我正在尝试使用IValueConverter禁用groupbox。这是我的错误:C# 项目与System.Windows for iValueConverter之间的差异,c#,wpf,C#,Wpf,有几个问题与此相关,但我的问题与我以前从未见过的另一个错误有关 我正在尝试使用IValueConverter禁用groupbox。这是我的错误:类型为“project.Data.IValueConverter”的对象无法应用于需要类型为“System.Windows.Data.IValueConverter”的属性。 我把IValueConverter放在一个名为Data的文件夹中。因此,我的项目结构如下: **Project** -Data IValueConverter.cs
类型为“project.Data.IValueConverter”的对象无法应用于需要类型为“System.Windows.Data.IValueConverter”的属性。
我把IValueConverter放在一个名为Data的文件夹中。因此,我的项目结构如下:
**Project**
-Data
IValueConverter.cs
App.xaml
MainWindow.xaml
这是我的IValueConverter.cs
using System;
using System.Globalization;
namespace simpliphy.Data
{
class IValueConverter
{
public class NegateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
}
}
}
我在xaml中使用它,如下所示:
<Window x:Class="project.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:project.Data"
Title="Main Window" Name="main">
<Window.Resources>
<local:IValueConverter x:Key="negate" />
</Window.Resources>
以及:
。。。
..
project.data和system.windows.data之间有什么区别?显然,我的转换器不能工作是有区别的 没有名为IValueConverter的类。将类直接放入命名空间中
namespace simpliphy.Data
{
public class NegateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
}
}
您试图使用定义为转换器的类IValueConverter,但该类未实现导致错误的接口
using System;
using System.Globalization;
namespace simpliphy.Data
{
public class NegateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
}
}
我不知道你为什么要在IValueConverter类中包装NegateConverter。使用上面的类,您可以将NegateConverter用作XAML资源,并将该资源用作转换器。谢谢!然而,在我的参考资料中,它说名称空间中不存在NegateConverter。实际上,我只是构建了我的项目,错误消失了。问题解决了!谢谢你的帮助,这是个愚蠢的错误。
using System;
using System.Globalization;
namespace simpliphy.Data
{
public class NegateConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool)
{
return !(bool)value;
}
return value;
}
}
}