WPF 编辑资源
您好,有什么方法可以从代码或通过某种绑定更改资源画笔吗? 我想要做的是在单击按钮时更改“主”画笔的颜色。
多谢!
编辑:
它是一个 GradientBrush,我如何更改其颜色?
myBrush.GradientStops[0].Color = Colors.Red;
只是给了我一个例外......有什么方法可以使颜色变化动画化,就像故事板一样?
Hi is there any way to change a Resource brush from code or via some binding?
what I want to do is change the color of my "main" brush when a button is clicked.
Thanks a lot!
Edit:
Its a GradientBrush how do i change the colors on that?
myBrush.GradientStops[0].Color = Colors.Red;
just gives me a exception... and is there any way to animate the color change, like a story board?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要动画更改,请尝试创建 Storyboard 并在其上调用 Begin。
(我将举一个例子)
编辑:看起来这是我的另一个 Silverlight != WPF 失败。 我似乎无法在 WPF 中实现它。
For animating the change, try creating a Storyboard and calling Begin on it.
(I'll go throw together an example)
edit: Looks like it's another Silverlight != WPF fail on my part. I cant seem to get it going in WPF.
如果您使用模型-视图-视图模型 (MVVM) 模式或类似模式,则可以将画笔颜色(或整个画笔)作为视图模型的属性并直接绑定到它。
在我(缺乏经验的)看来,资源不应该在运行时改变。 如果要更改,请绑定它。
(edit2:从 Silverlight 样式的顶级 UserControl 更改为 WPF Window。正如 Ray Booysen 在评论中指出的那样,WPF 中的 UserControl 将通过 DependencyProperty 公开颜色,而不是将其绑定到 ViewModel。)
XAML:
Viewmodel 类:
XAML.cs:
If you're using the Model-View-ViewModel (MVVM) pattern or something similar, you could make the brush colour (or the entire brush) a property of your viewmodel and bind directly to it.
In my (inexperienced) opinion, resources shouldnt change at runtime. If it's going to be changing, bind it.
(edit2: Changed from Silverlight-style top-level UserControl to WPF Window. As Ray Booysen noted in the comments, a UserControl in WPF would expose the colour via a DependencyProperty, not have it bound to a ViewModel.)
XAML:
Viewmodel class:
XAML.cs: