gpt4 book ai didi

c# - IValueCONverter 不工作

转载 作者:行者123 更新时间:2023-11-30 16:34:54 25 4
gpt4 key购买 nike

在谷歌上搜索这个问题几个小时,但看不出我哪里出错了。

我有以下转换器,它只返回 Brushes.Red(也尝试过 Colors.Red),但仍然没有成功。

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>

我在顶部设置了以下命名空间:

xmlns:local="clr-namespace:Dashboard"

现在我有以下绑定(bind)到堆栈面板的类:

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));
}
}
}

数据绑定(bind)(Value)工作得很好,但转换器不想启动,我试图在 covnerter 的 Convert 方法中设置一个断点,但在调试时没有触发,它看起来就像如果我的调试器没有被调用。

任何人都可以阐明这一点吗?

最佳答案

令我惊讶的是,您说绑定(bind)本身有效,因为“值”和“颜色”是字段,绑定(bind)到字段不应该有效。

关于c# - IValueCONverter 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2244816/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com