WPF 编辑资源

发布于 2024-07-13 13:37:05 字数 248 浏览 9 评论 0原文

您好,有什么方法可以从代码或通过某种绑定更改资源画笔吗? 我想要做的是在单击按钮时更改“主”画笔的颜色。

多谢!

编辑:

它是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那一片橙海, 2024-07-20 13:37:05

要动画更改,请尝试创建 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.

攒一口袋星星 2024-07-20 13:37:05

如果您使用模型-视图-视图模型 (MVVM) 模式或类似模式,则可以将画笔颜色(或整个画笔)作为视图模型的属性并直接绑定到它。

在我(缺乏经验的)看来,资源不应该在运行时改变。 如果要更改,请绑定它。

(edit2:从 Silverlight 样式的顶级 UserControl 更改为 WPF Window。正如 Ray Booysen 在评论中指出的那样,WPF 中的 UserControl 将通过 DependencyProperty 公开颜色,而不是将其绑定到 ViewModel。)

XAML:

<Grid x:Name="LayoutRoot">
    <Grid.Background>
        <SolidColorBrush Color="{Binding BackgroundColor}" />
    </Grid.Background>
    ...

Viewmodel 类:

public class MyViewModel : INotifyPropertyChanged
{
    public Color BackgroundColor
    {
        get { ... }
        set { ... } // fire PropertyChanged event
    }
    ...

XAML.cs:

public partial class MyWindow : Window
{
     private MyViewModel m_viewmodel;

     public MyWindow()
     {
          InitializeComponent();
          viewmodel = new MyViewModel();
          this.LayoutRoot.DataContext = viewmodel;
     }

     private void ButtonClick(object sender, RoutedEventArgs e)
     {
         this.viewmodel.BackgroundColor = Color.Red;
     }
     ...

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:

<Grid x:Name="LayoutRoot">
    <Grid.Background>
        <SolidColorBrush Color="{Binding BackgroundColor}" />
    </Grid.Background>
    ...

Viewmodel class:

public class MyViewModel : INotifyPropertyChanged
{
    public Color BackgroundColor
    {
        get { ... }
        set { ... } // fire PropertyChanged event
    }
    ...

XAML.cs:

public partial class MyWindow : Window
{
     private MyViewModel m_viewmodel;

     public MyWindow()
     {
          InitializeComponent();
          viewmodel = new MyViewModel();
          this.LayoutRoot.DataContext = viewmodel;
     }

     private void ButtonClick(object sender, RoutedEventArgs e)
     {
         this.viewmodel.BackgroundColor = Color.Red;
     }
     ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文