Solved

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

Posted on 2012-04-03
6
557 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now