C# IValueCOnverter不工作

C# IValueCOnverter不工作,c#,data-binding,converter,ivalueconverter,C#,Data Binding,Converter,Ivalueconverter,在谷歌上搜索这个问题已经好几个小时了,我看不出哪里出了问题 我有下面的转换器,它只返回画笔。红色(也尝试过颜色。红色),但仍然没有运气 public class ColorConverter : IValueConverter { private static ColorConverter instance = new ColorConverter(); public static ColorConverter Instance { get

在谷歌上搜索这个问题已经好几个小时了,我看不出哪里出了问题

我有下面的转换器,它只返回画笔。红色(也尝试过颜色。红色),但仍然没有运气

public class ColorConverter : IValueConverter
{
    private static ColorConverter instance = new ColorConverter();
    public static ColorConverter Instance
    {
        get
        {
            return instance;
        }
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Brushes.Red;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}
现在,在我的xaml中,我有以下代码:

<StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding Value}" TextAlignment="Center" Foreground="{Binding Path=color, Converter={x:Static local:ColorConverter.Instance}}" Margin="2"/>
</StackPanel>
现在,我有以下绑定到堆栈面板的类:

public class MyClass : INotifyPropertyChanged
{
    public String Value;
    public Color color;

    // Declare the PropertyChanged event
    public event PropertyChangedEventHandler PropertyChanged;
    public void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}
数据绑定(值)工作得非常好,但转换器不想启动,我尝试在covnerter的Convert方法中设置断点,但调试时没有触发,似乎没有调用我的调试器


有人能解释一下吗?

好吧,下面是我在项目中是如何做到这一点的。我修改了我的代码以反映您试图做的事情。我希望有帮助。我无法回答为什么你的单例方法不起作用

类别:

public class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return Brushes.Red;
    }
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new Exception("The method or operation is not implemented.");
    }
}
在my UserControl.Resources元素中:

<UserControl.Resources>
    <local:ColorConverter x:Key="MyColorConverter" />
</UserControl.Resources>

StackPanel元素:

<StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding Value}" TextAlignment="Center" Foreground="{Binding Path=color, Converter={StaticResource MyColorConverter}}" Margin="2"/>
</StackPanel>


另外,您是否检查了输出窗口以查看是否有任何错误?你也应该阅读。实际上,她有一个关于IValueConverters的专门章节,也许有一天会对你有用。

我很惊讶你说绑定本身可以工作,因为“值”和“颜色”是字段,而绑定到字段不应该工作。

谢谢!进入我的输出窗口后发现问题。我不知道,要使变量成为属性,必须向其添加{set;get;}。所以我没有它们,输出抱怨它找不到“颜色”属性。非常感谢你的帮助,谢谢你的链接。添加到收藏夹中!啊,是的,您确实使用了字段而不是属性。我以前犯过这个错误(显然,我又犯了一次)是的,我甚至懒得看他们的声明,因为它听起来像一切,但转换器正在工作。:)
<StackPanel Orientation="Vertical">
    <TextBlock Text="{Binding Value}" TextAlignment="Center" Foreground="{Binding Path=color, Converter={StaticResource MyColorConverter}}" Margin="2"/>
</StackPanel>