【发布时间】:2012-10-23 07:49:49
【问题描述】:
我正在 WPF 中编写一个用户控件,这是我自己的第一个控件。 供您参考,我正在使用 Telerik 控件。
我的用户控件只是一个 Grid,它只包含 2 个 GridViews。
现在我想通过设置前景和背景来给某人设置GridViews 样式的可能性。
我都是这样设置的:
Background="{Binding ElementName=Grid, Path=DarkBackground}"
Foreground="{Binding ElementName=Grid, Path=LightForeground}"
我的代码是:
public static DependencyProperty LightForegroundProperty = DependencyProperty.Register( "LightForeground", typeof( Brush ), typeof( ParameterGrid ) );
public Brush LightForeground
{
get
{
return (Brush)GetValue( LightForegroundProperty );
}
set
{
SetValue( LightForegroundProperty, value );
}
}
public Brush DarkBackground
{
get
{
return (Brush)GetValue( DarkBackgroundProperty );
}
set
{
SetValue( DarkBackgroundProperty, value );
}
}
问题是我的前景,背景值在运行时被忽略了。 为 Foreground 设置一个固定值会带来预期的结果。
我没有发现我的错误,有人知道吗???
【问题讨论】:
-
现在我知道这是一个绑定问题。 Snoop 告诉我:> System.Windows.Data 错误:4:找不到与引用“ElementName = Grid”进行绑定的源。 BindingExpression:Path=LightForeground ;数据项=空;目标元素是'TextBlock'(名称='');目标属性是“前景”(类型“画笔”)
标签: c# wpf user-controls wpf-controls foreground