Solved

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

Posted on 2012-04-03
6
564 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Monthly Recap

May was a big month for new releases from Linux Academy! Take a look at what our team built recently in our blog. You can access the newest releases from our blog.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 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