2

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?

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Peter
  • 37,042
  • 39
  • 142
  • 198

2 Answers2

2

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.

geofftnz
  • 9,954
  • 2
  • 42
  • 50
-1

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;
     }
     ...
geofftnz
  • 9,954
  • 2
  • 42
  • 50
  • If the MVVM pattern approach is taken, you wouldn't even have a Click handler. Instead, you'd expose a Command in your "MyViewModel" class (call it ChangeMainBrushCommand or whatever), which runs the change logic and have your View's "Change Button" bind its Command property to your custom command. – Adrian Feb 09 '09 at 20:25
  • Yep, but in Silverlight I cant do commands, so I prefer not to think about them :( – geofftnz Feb 09 '09 at 20:29
  • There, there. They'll remember you web guys... eventually. – Adrian Feb 09 '09 at 20:45
  • Haha... the irony is that I'm a WinForms coder and havent touched web stuff in about 6 years. It just happens that the first project to land on my desk after I decided to make the move to WPF had to be deployed over the web, so Silverlight it was... – geofftnz Feb 09 '09 at 20:57
  • Woah,no.If you're doing a UserControl just expose a dependencyProperty,not have a ViewModel backing for UI.Follow the same pattern for usercontrols as with normal controls.The consumer of the control can write then an animation to change the brush using the new dependency property. – Ray Booysen Feb 09 '09 at 23:47
  • Sorry, the code I posted was Silverlight2, where Window doesnt exist and you have to use a UserControl for top-level UI. In WPF you're absolutely right - a UserControl would expose a dependency property. – geofftnz Feb 10 '09 at 00:13