项目和系统的区别.Windows的iValueConverter
本文关键字:Windows iValueConverter 区别 项目 系统 | 更新日期: 2023-09-27 18:05:30
有一些与此相关的问题,但我的问题涉及到一个我以前没有见过的不同错误。
我试图使用IValueConverter来禁用组框。这是我的错误:An object of the type "project.Data.IValueConverter" cannot be applied to a property that expects the type "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>
:
...
<GroupBox Header="Device Stats" IsEnabled="{Binding ElementName=tabBERT, Path=IsSelected,Converter={StaticResource negate}}" HorizontalAlignment="Left" Margin="655,10,0,0" Width="191" Height="469" VerticalAlignment="Top">
..
project和project的区别是什么?数据和系统。windows。数据?显然有一个区别,我的转换器不会工作!
您正在尝试使用您定义为转换器的类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资源,并将该资源应用于转换器。
没有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;
}
}
}