我正试图找到一种通用的方法来设置UIElement上的背景属性。获取UIElement或发件人的背景
我没有多少运气...
这里是我迄今(尝试使用反射来获取BackgroundProperty)。
Action<UIElement> setTheBrushMethod = (UIElement x) =>
{
var brush = new SolidColorBrush(Colors.Yellow);
var whatever = x.GetType().GetField("BackgroundProperty");
var val = whatever.GetValue(null);
((UIElement)x).SetValue(val as DependencyProperty, brush);
brush.BeginAnimation(SolidColorBrush.ColorProperty, new ColorAnimation(Colors.White, TimeSpan.FromSeconds(3)));
};
setTheBrushMethod(sender as UIElement);
的事情是......它适用于像TextBlock的,但对于像一个StackPanel或按钮不起作用。
“无论”结束为StackPanel或Button为空。
我也觉得应该有一个简单的方法来一般地设置背景。我错过了明显吗?
背景似乎只能在System.Windows.Controls.Control上使用,但我无法转换为此。
嗯....你有没有考虑使用样式? – failedprogramming 2013-04-26 00:36:31