Solved

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

Posted on 2012-04-03
6
560 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
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…

777 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