tags:

views:

463

answers:

3
+1  Q: 

WPF Edit Resource

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?

+1  A: 

Edit: Changed to LinearGradientBrush.

Not sure why you are getting an exception. This particular syntax works fine for me: XAML:

<LinearGradientBrush x:Key="MainBrush" StartPoint="0, 0.5" EndPoint="1, 0.5" >
    <GradientBrush.GradientStops>
        <GradientStop Color="Blue" Offset="0" />
        <GradientStop Color="Black" Offset="1" />
    </GradientBrush.GradientStops>
</LinearGradientBrush>

Code:

LinearGradientBrush myBrush = FindResource("MainBrush") as LinearGradientBrush;
myBrush.GradientStops[0].Color = Colors.Red;

If you would like to "animate" the color of a GradientStop, I recommend the MSDN article on the subject.

KP Adrian
Im doing this while the brush is in use i dont know if this matters?
Petoj
No this doesn't matter. If you are using the brush, the examples above will immediately update the value and you will see it change (obviously in my example, the color will change without any transition, so you will need to read the animation doc if you want to fluidly change the color).
KP Adrian
I still get a exception on the line "myBrush.GradientStops[0].Color = Colors.Red;"InvalidOperationExceptionCannot set a property on object '#FF007EFF;0' because it is in a read-only state.
Petoj
I am trying this and getting the same InvalidOperationException Petoj mentioned...I can't seem to get this to work. I have a question about it here: http://stackoverflow.com/questions/786183/wpf-changing-resources-colors-from-the-app-xaml-during-runtime
Andreas Grech
A: 

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
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.
KP Adrian
Yep, but in Silverlight I cant do commands, so I prefer not to think about them :(
geofftnz
There, there. They'll remember you web guys... eventually.
KP Adrian
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
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
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
+2  A: 

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
ok np :P you tryed atleast
Petoj