前台属性绑定不起作用 [英] Foreground property binding is not working
问题描述
我有一个文本块,文本的前景颜色应该按值改变。但是效果不好。
I have a textblock, the text's foreground's color should change by the value. However it is not working well.
<TextBlock Text="{Binding Path=Foo}"
Foreground="{Binding Path=Foo, Converter={StaticResource myConverter}" />
转换器部分喜欢
The converter part likes
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
var temp = (int)value;
if (temp <100 && temp>50)
{
return "#ff23d3";
}
if(temp>200)
return "da2367";
return new SolidBrush(Colors.Green);
}
但是虽然值不同,但只返回一种颜色。
< b>我尝试过:
我在代码中设置了断点。它确实到了正确的位置。
也许类型转换错了?
But although the values are different, it only return one color.
What I have tried:
I set the break point in the code. It did reach the right place.
Maybe type conversion is wrong?
推荐答案
首先,Foreground [ ^ ] 依赖属性 [ ^ ]任何 WPF控件 [ ^ ]期待< a href =https:// msd n.microsoft.com/en-us/library/system.windows.media.brush(v=vs.110).aspx\">画眉 [ ^ ]。
有了这些知识,我们现在可以查看您的转换器:
First things first, the Foreground[^] Dependency Property[^] of the of any WPF Control[^] is expecting a Brush[^].
Armed with this knowledge, we can now look at your Converter:
public object Convert(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
var temp = (int)value;
if (temp <100 && temp>50)
{
return "#ff23d3";
}
if(temp>200)
return "da2367";
return new SolidBrush(Colors.Green);
}
1. #ff23d3
= string hex颜色值(不是颜色对象),不是任何类型的画笔。
2. da2367
是一个字符串。与第一个不一样,但仍然不是画笔。
3. 新的SolidBrush(Colors.Green )
- 这是一种纯色画笔。这将有效。
因此,要修复,您需要将颜色值转换为颜色类 [ ^ ]可以与刷子一起使用:
1. "#ff23d3"
= string hex color value (not color object), not a Brush of any kind.
2. "da2367"
is a string. Not the same as the first, but still not a Brush.
3. new SolidBrush(Colors.Green)
- This is a Solid Color Brush. This will work.
So, to fix, you need to convert a color value to a Color class[^] that can be used with a Brush:
using System.Windows.Media;
Color color = (Color)ColorConverter.ConvertFromString("#FFFF23D3");
现在你有了一个Color对象,你可以将它应用到的SolidColorBrush [< a href =https://www.google.com.au/search?safe=off&ei=qqEOWv-WO8it8QWjv5Y4&q=WPF+SolidColorBrush+class&oq=WPF+SolidColorBrush+class&gs_l=psy-ab。 3..0i30k1.770.1181.0.2083.2.2.0.0.0.0.495.755.2-1j0j1.2.0 .... 0 ... 1.1.64.psy-ab..0.2.753 ... 0i7i30k1.0.wdB_ -YzlZuEtarget =_ blanktitle =New Window> ^ ]:
Now that you have a Color object you can apply it to a SolidColorBrush[^]:
return new SolidBrush(color);
所以,你的固定转换呃将看起来像:
So, your fixed Converter will look like:
public object Convert(object value,
Type targetType,
object parameter,
System.Globalization.CultureInfo culture)
{
var temp = (int)value;
if (temp <100 && temp>50)
return new SolidColorBrush(
(Color)ColorConverter
.ConvertFromString("#FFFF23D3"));
if(temp>200)
return new SolidColorBrush(
(Color)ColorConverter
.ConvertFromString("#FFDA2367"));
return new SolidColorBrush(Colors.Green);
}
这篇关于前台属性绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!