WPF 转换器从绑定中获取奇怪的值
我的绑定设置如下
XAML
<TextBlock Text="{Binding Path=Color, Converter={StaticResource ColorToStringConverter}}" />
C#:显示 Color
是什么
public System.Windows.Media.Color Color
{
get
{
var color = new HSLColor { Hue = this.Hue, Saturation = this.Saturation, Luminosity = this.Luminosity };
string strColor = color.ToRGBString();
return new System.Windows.Media.Color {
R = byte.Parse(strColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
G = byte.Parse(strColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
B = byte.Parse(strColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber)
};
}
set { SetValue(ColorProperty, value); }
}
转换器
public class ColorToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Color color = (Color)value;
return color.ToString();
}
}
但我的转换器正在获取 value
就像
value = "{Name=0, ARGB=(0, 0, 0, 0)}"
我希望它是一个 System.Windows.Media.Color
为什么我会得到这个?
基本上,我有 3 个用于绑定到 DependencyProperties
的 HSL 值的 Silder,每个 Silder 都附加了一个 PropertyChangedCallback
new PropertyChangedCallback(HSLValuePropertyChanged)
看起来
protected void HSLValueChanged()
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Color"));
}
基本上它的目的是更新绑定到依赖属性的控件<代码>颜色。这个想法是 get
应该针对属性 Color
运行,它会根据 HSL 属性创建新颜色。问题似乎是即使我更改 HSL 值, get 也不会运行。
更新
所以我尝试在发生异常的情况下只返回value
,但我在文本框中什么也没有得到,所以我做了value.toString()
> 一直都是Color [Empty]
。我做错了什么?
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
Color color = (Color)value;
return color.ToString();
} catch (Exception) {
return value.ToString();
}
}
I have setup binding as follows
XAML
<TextBlock Text="{Binding Path=Color, Converter={StaticResource ColorToStringConverter}}" />
C#: Showing what Color
is
public System.Windows.Media.Color Color
{
get
{
var color = new HSLColor { Hue = this.Hue, Saturation = this.Saturation, Luminosity = this.Luminosity };
string strColor = color.ToRGBString();
return new System.Windows.Media.Color {
R = byte.Parse(strColor.Substring(0, 2), System.Globalization.NumberStyles.HexNumber),
G = byte.Parse(strColor.Substring(2, 2), System.Globalization.NumberStyles.HexNumber),
B = byte.Parse(strColor.Substring(4, 2), System.Globalization.NumberStyles.HexNumber)
};
}
set { SetValue(ColorProperty, value); }
}
Converter
public class ColorToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Color color = (Color)value;
return color.ToString();
}
}
But my converter is getting value
like
value = "{Name=0, ARGB=(0, 0, 0, 0)}"
I'd expect it to be a System.Windows.Media.Color
why am I getting this?
Basically, I have 3 Silders for HSL values bound to DependencyProperties
, each have a PropertyChangedCallback
attached to them
new PropertyChangedCallback(HSLValuePropertyChanged)
It looks like
protected void HSLValueChanged()
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("Color"));
}
Basically its purpose is to update controls bound to the dependency property Color
. The idea is that get
should run for property Color
which creates a new color from HSL properties. The problem it seems is that the get does not run even when I change HSL values.
UPDATE
So I tried to return just value
in the case of an exception, I got nothing in the textbox, so i did value.toString()
got Color [Empty]
all the time. What did I do wrong?
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
try
{
Color color = (Color)value;
return color.ToString();
} catch (Exception) {
return value.ToString();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您似乎正在使用 ColorDependencyProperty。
如果是这样,我认为绑定可能永远不会真正通过访问器(获取/设置)...
您应该在每个 H/S/L 属性更改回调上设置新颜色(通过 setvalue 方法在 dependencyProperty 中) ...
我希望我很清楚:-)
You seem to be using the ColorDependencyProperty.
If so, I think the binding may never actually go trough the accessors (get/set) ...
You should set the new color (in the dependencyProperty via the setvalue method) on each of your H/S/L properties change call back...
I hope i am clear :-)
当绑定到 DependencyProperty 时,绑定不使用属性 getter 来获取值,因此,就绑定而言,您放入
Color
属性 getter 中的所有代码都会被跳过。坚持将 getter 定义为
get { return (XXX)GetValue(MyProperty); }
并找到另一种方法。When bound on a DependencyProperty, bindings dont use property getters to obtain values, so, all the code you put in the
Color
property getter is skipped as far as bindings are concerned.Stick to defining your getters as
get { return (XXX)GetValue(MyProperty); }
and find another way around.