Solved

WPF Bindings to MySettings.Default Not Updating When Property Changes (VB.Net)

Posted on 2012-04-03
6
561 Views
Last Modified: 2012-04-04
Hi,

I recently discovered the User Settings functionality of Visual Studio and thought I would give it a try so that my users can customise colours, fonts etc.

While playing to gain familiarity I have created a Setting called 'ButtonColour' as a system.drawing.color and then, in XAML, created a binding to this setting in the Control Template for the Buttons in my project, using an iValueConverter to convert the color to a System.Windows.Media.Brush.

This works as it should.

I then added a ComboBox, which contains all of the non-system KnownColors, and set the SelectionChanged event to update the ButtonColor setting to that selected from the Combo.

My problem is that although ButtonColor setting is changed and saved, the Buttons on the screen using the bound Control Template do not change colour until they are reloaded.

I would have though that MySettings would implement INotifyPropertyChanged, but it seems not - or if it is it is not firing.

What is the 'correct' way to get my buttons to change colour in real-time as the ButtonColor setting changes?

All help appreciated.

Cheers

Sim
0
Comment
Question by:Simon482
  • 3
  • 2
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37805405
Does it update if you call InvalidateVisual function on the button?

http://msdn.microsoft.com/en-us/library/system.windows.uielement.invalidatevisual.aspx
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37805417
Are you calling the Save method for the settings?  Nothing will get saved if you don't.

My.Settings.Default.Save()
0
 

Author Comment

by:Simon482
ID: 37805524
Thanks for your replies

@CodeCruiser - I have given this a try and no, I am afraid not.

@TheLearnedOne - Yes, I am saving the change and if I restart the app I can see it has taken effect - I just would like the UI to reflect the changes in real-time.

I have a workaround, but it seems a little convoluted.

I have created a Class name 'ButtonStyle' which inherits from DependencyObject and Implements INotifyPropertyChanged, and then created a dependcyproperty called ColourProperty.

I then bound the ColourProperty to the ButtonColour Setting, and bound my ControlTemplate to the ColourProperty of my new Class.

When I change the color from the Combo I refresh the binding of ColourProperty and this is reflected on the UI instantly.

However, I feel this is all a bit unnecesary and suspect there is an easier way... Opinions?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 37805670
I misunderstood your question.

"I would have though that MySettings would implement INotifyPropertyChanged, but it seems not - or if it is it is not firing."

INotifyPropertyChanged is a new construct, and MySettings is an old one.  I haven't tried extending the Settings class to add new behavior, and I am not sure if there is a reason to want to.  If you look at Aspect Oriented Programming (AOP), and dependency injection, you can add behavior where it didn't exist before.

Introduction to Spring's Aspect Oriented Programming(AOP)
http://www.javabeat.net/articles/51-introduction-to-springs-aspect-oriented-programminga-1.html
0
 

Author Closing Comment

by:Simon482
ID: 37805820
OK, thanks, plenty of further reading there...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 37805839
BTW, that was an article that talked about the concepts of AOP, as it was a Java-based implementation.  There are .NET flavors, such as Post Sharp.

AOP on .NET
http://www.sharpcrafters.com/aop.net

An easy way to implement what you want is to use a custom class that implements the INotifyPropertyChanged that you need, and has a persistence mechanism, rather than trying to bend something in that direction.
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question