.NET Blog

Tony Cavaliere

 
My Favourite Albums
  And the Grappa wins.
E-mail me Send mail
Add to Technorati Favorites AddThis Feed Button

Subscribe to Cynot Why Not


Recent posts

Disclaimer

Hey unlike other bloggers I stand by what I say but just in case. The opinions expressed herein are my own except on Tuesday when the second card is not turned up otherwise it ain't worth squat.

© Copyright 2010

Using an Intermediary to implement binding between Silverlight controls

WPF has built-in support for binding UI elements. For example, when the user changes the position of the slider the width of the button changes, all done through binding in XAML. Unfortunately, Silverlight does not have this built-in support. So what can be done?

Using an intermediary class and generics it is fairly simple to roll your own. Let's take a look at how it works.

Using VS2008 create a Silverlight application. I called the project BindingBetweenUIControls, rename the TestPage's to default and am using VB.NET. Next, in the Silverlight project, add a class called IntermediaryUIBinding.vb. Figure 1 shows the solution explorer.

Figure 1: Solution Explorer

solution  

Edit the class InterediaryUIBinding.vb. Listing 1 shows the complete listing of this class.

Listing 1: The class IntermediaryUIBinding used to bind UI controls.

    1 Imports System.ComponentModel

    2 

    3 'Intermediary class used to binding to UI properties of type double.

    4 Public Class IntermediaryUIBinding(Of T)

    5     Implements INotifyPropertyChanged

    6 

    7     Public Event PropertyChanged( _

    8         ByVal sender As Object, _

    9         ByVal e As System.ComponentModel.PropertyChangedEventArgs) _

   10         Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

   11 

   12     Private _IntermediaryValue As T

   13     Public Property IntermediaryValue() As T

   14         Get

   15             Return _IntermediaryValue

   16         End Get

   17         Set(ByVal value As T)

   18             _IntermediaryValue = value

   19             RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("IntermediaryValue"))

   20         End Set

   21     End Property

   22 

   23     Public Sub New(ByVal value As T)

   24         IntermediaryValue = value

   25     End Sub

   26 

   27 End Class

This generic class implements INotifyPropertyChanged which is a requirement if you want to provide two way data binding. Please refer to the previous post Silverlight Data Binding, using INotifyPropertyChanged and ObservableCollection. The generic implementation permits this class to be used for UI properties of various types. In this example we will be binding a double value. The actual class implementation is extremely simple containing a single property that raises the PropertyChanged event whenever the value is modified. This event is used by the binding infrastructure to notify the UI of data changes.

The UI for this Silverlight control is shown in listing 2.

Listing 2: Page.xaml with the UI data binding markup.

    1 <UserControl x:Class="BindingBetweenUIControls.Page"

    2    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    3    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    4    >

    5     <Grid x:Name="LayoutRoot" Background="Bisque" Width="400" Height="300">

    6         <Grid.RowDefinitions>

    7             <RowDefinition Height="150"/>

    8             <RowDefinition/>

    9             <RowDefinition/>

   10         </Grid.RowDefinitions>

   11         <TextBlock FontSize="18" TextWrapping="Wrap" Margin="10">

   12             <Run>Using an intermediary object you can bind properties of controls.</Run>

   13             <Run>Change the slider position and watch the button width change.</Run>

   14         </TextBlock>

   15         <Slider

   16            x:Name="bindingSlider"

   17            Value="{Binding IntermediaryValue, Mode=TwoWay}"

   18            Width="300" Minimum="100" Maximum="300"

   19            Grid.Row="1" />

   20         <Button

   21            x:Name="bindingButton"

   22            Width="{Binding IntermediaryValue}" Height="50"

   23            Content="Binding UI" Grid.Row="2" />

   24     </Grid>

   25 </UserControl>

The markup contains two controls; a slider and a button. The slider, line 17,  has it's Value attribute bound to the IntermediaryValue property and uses two way data binding. Similarly , the button control, line 22, has it's Width attribute bound to the same IntermediaryValue property but does not use two way data binding. The two way binding for the slider is necessary since we want the underlying data stored in the property  IntermediaryValue to change whenever the slider position is changed by the user.

Finally, all that needs to be done is to set the DataContext of a parent control. This is easily done in the code behind of Page.xaml and is shown in listing 3.

Listing 3: Page.xaml,vb Codebehind

    1 Partial Public Class Page

    2     Inherits UserControl

    3 

    4     Public Sub New()

    5         InitializeComponent()

    6     End Sub

    7 

    8     Private Sub Page_Loaded( _

    9         ByVal sender As Object, _

   10         ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded

   11         LayoutRoot.DataContext = New IntermediaryUIBinding(Of Double)(bindingSlider.Value)

   12     End Sub

   13 

   14 End Class

Fairly simple, line 11 sets the DataContext of the grid control to an instance of the Intermediary class specifying a generic type of Double. The constructor parameter specifies the initial value of the slider. Now when the users changes the position of the slider the binding infrastructure will update the underlying data store, i.e., the property  IntermediaryValue and since this property is bound to the button's width it will change accordingly.

That's it binding between Silverlight UI controls.

This post was inspired by a presentation by Dan Borkowski at the Toronto Silverlight User Group.

If you would like to see this code in action or would like to download the code then visit my Silverlight Demo Gallery.

Guess the movie

Where do you think you're going? Nobody's leaving. Nobody's walking out on this fun, old-fashioned family Christmas. No, no. We're all in this together. This is a full-blown, four-alarm holiday emergency here. We're gonna press on, and we're gonna have the hap, hap, happiest Christmas since Bing Crosby tap-danced with Danny fucking Kaye. And when Santa squeezes his fat white ass down that chimney tonight, he's gonna find the jolliest bunch of assholes this side of the nuthouse.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Silverlight
Posted by CynotWhyNot on Thursday, December 04, 2008 3:30 PM
Permalink | Comments (68) | Post RSSRSS comment feed

Related posts

Comments

Busby SEO Test us

Thursday, December 25, 2008 10:06 AM

Busby SEO Test

yeah i agree with you

Urban Net Zone us

Friday, March 06, 2009 12:45 PM

Urban Net Zone

maybe i can use this codes here

payday online us

Tuesday, September 15, 2009 1:24 AM

payday online

I havent any word to appreciate this post.....Really i am impressed from this post....the person who create this post it was a great human..thanks for shared this with us.

payday loans us

Monday, September 21, 2009 2:36 PM

payday loans

We are a group of volunteers and starting a new initiative in a community. Your blog provided us valuable information to work on.You have done a marvellous job!

discount bag US

Tuesday, December 01, 2009 12:31 PM

discount bag

I find your blog in google. And I will be back next time, thanks.

nike air jordan US

Thursday, December 24, 2009 3:47 PM

nike air jordan

This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.

discount designer handbags US

Wednesday, December 30, 2009 6:48 AM

discount designer handbags

If you need to buy discount gucci bags, you can just type bagsvendor.com in you browser address line and find out the gucci bags on big sale in discounted price. The information below is only introducing the discount news about gucci bags and gucci handbags. A woman leads the world accounting her gucci bag. But then we must also remember that there is a well discount news that has to see. It is a common complaint that women having their share is not large enough to hold all your belongings necessary, such as discount gucci bags, designer bags. This gucci bags you can find in bagsvendor.com is that the person is unable to organize things properly. Each discount bag has a clear purpose and objects you choose to take the bag must be compatible with the purposes for which it is purchased in your mind. We can meet the wide range of needs that are part of the common things for a trip on a beautiful handbag, bags or purses. Thus, while the discounted gucci bags needs, here are some points that may be useful to organize the contents of the discount gucci bag. Even it is a discounted price gucci bags, but it has high quality. The quality of materials used with the gucci bag is leather, suede or cotton. These materials can be sure that they are not less than 100% authentic and of excellent quality discount bags. Accessories and decorative items from the discount gucci bag, such as hinges, chains, rings and belt buckles are higher that other bags. Some designers in the Gucci company will even give as a repair or replacement free of charge in a period of time to ensure that their discount handbags are offered in a different league than the shares that you can find at any mall or online store. Designer Gucci bags can be changed to discounted gucci bags. This is a big news about handbags on sale. Brands are just called by many famous fashion people, and can be done with any discounted news. Just find more in bagsvendor.com

air jordan US

Friday, January 01, 2010 2:46 AM

air jordan

In winnersneaker, these Nike Air Jordan shoes were associated with sport, fashion and basketball. The Nike Air Jordan sneakers were a way to hide some of the smaller sizes, or raise an important NBA star to even higher levels to the count. Nike Air Jordan shoes, but also less fashionable of Air Jordan history. Which were commonly used by high-ranking customers in NBA during the 19th century and were used in the 20th century to let the putrid mud in the streets, all you know is that you can find many discounted Air Jordan shoes in winnersneaker.com. Existing Air Jordan retro sneakers in the United States and Europe retails do not feel in fashion until the new Air Jordan shoes come in the street. At first the Air Jordan 1 shoes important wearing for young sport men, but once reigned basketball, Jordan shoes became the must-have fashion accessory for young people in the basketball playground. These Air Jordan original shoes were all to make a statement vividly. Air Jordans like Air Jordan 23 and Air Jordan 6 wore basketball shoes kiss in the context of his people larger than life. Wish you find a suitable Air Jordan shoes in winnersneaker.com.

digest it us

Friday, February 19, 2010 11:30 AM

digest it

One must learn by doing the thing. For though you think you know it, you have no certainty until you try.

designer handbags US

Sunday, March 07, 2010 4:05 PM

designer handbags

Yeah, you are right, I agree with you.

designer handbag US

Monday, March 08, 2010 10:55 PM

designer handbag

This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.

designer handbags sale US

Tuesday, March 09, 2010 2:35 PM

designer handbags sale

Substantially, the article is in reality the sweetest on this precious topic. I harmonise with your conclusions and will thirstily look forward to your upcoming updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing. I will directly grab your rss feed to stay abreast of any updates. Pleasant work and much success in your business efforts!

nba stars sneakers US

Wednesday, March 10, 2010 9:45 PM

nba stars sneakers

You have a point. Very insightful. A nice different perspective

shoes china US

Wednesday, March 10, 2010 11:29 PM

shoes china

Hey, guy, your blog is nice. It can bring me many useful information.

discount handbags sale US

Thursday, March 11, 2010 2:48 AM

discount handbags sale

Funny, I actually had this on my mind a few days ago and now I come across your blog.

basketball sheos US

Thursday, March 11, 2010 6:48 AM

basketball sheos

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

shoes wholesale china US

Friday, March 12, 2010 12:07 AM

shoes wholesale china

This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.

shoes china US

Friday, March 12, 2010 2:51 PM

shoes china

Your blog is perfect, and I like this article. I find the information I need. I think I can find more useful information here, thanks.

air force 1s US

Sunday, March 14, 2010 3:28 AM

air force 1s

Amazing article. Bookmarked it already. Best regards, Ken.

discount sheos wholesale US

Sunday, March 14, 2010 3:29 AM

discount sheos wholesale

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

designer bags US

Sunday, March 14, 2010 4:06 AM

designer bags

Funny, I actually had this on my mind a few days ago and now I come across your blog.

nba shoes sale US

Sunday, March 14, 2010 4:51 AM

nba shoes sale

This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.

adidas shoes US

Sunday, March 14, 2010 4:51 AM

adidas shoes

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

designer handbag US

Sunday, March 14, 2010 4:55 AM

designer handbag

Your blog is perfect, and I like this article. I find the information I need. I think I can find more useful information here, thanks.

kobe shoes sale US

Sunday, March 14, 2010 6:21 AM

kobe shoes sale

While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find. I do appreciate that youve added relevant and intelligent commentary here though. Thank you!

air max paypal US

Sunday, March 14, 2010 8:08 AM

air max paypal

Your blog seems interesting.Regards,Kevin.

air max 87 US

Monday, March 15, 2010 12:58 AM

air max 87

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

paul sneakers US

Monday, March 15, 2010 5:59 PM

paul sneakers

Thanks for your article.

designer handbags sale US

Thursday, March 25, 2010 5:36 AM

designer handbags sale

Your blog seems interesting.Regards,Kevin.

air max sale US

Thursday, March 25, 2010 7:35 AM

air max sale

Your blog is perfect, and I like this article. I find the information I need. I think I can find more useful information here, thanks.

james shoes sale US

Thursday, March 25, 2010 10:53 AM

james shoes sale

Your blog seems interesting.Regards,Kevin.

air jordan sale US

Thursday, March 25, 2010 4:33 PM

air jordan sale

Funny, I actually had this on my mind a few days ago and now I come across your blog.

basketball sheos US

Thursday, March 25, 2010 6:26 PM

basketball sheos

I was thinking of using BlogEngine but then I saw that most of the sites I looked either had comments full of spam or they had simply closed the comments altogether. I hope that you have been able to combat the spam because at the moment it is something that is making me stay away from BE.

air yeezy sneakers US

Friday, March 26, 2010 6:54 AM

air yeezy sneakers

Comfortabl y, the article is in reality the best on this valuable topic. I harmonise with your conclusions and will thirstily look forward to your coming updates. Just saying thanks will not just be sufficient, for the wonderful clarity in your writing. I will instantly grab your rss feed to stay privy of any updates. Fabulous work and much success in your business dealings!

basketball sheos US

Friday, March 26, 2010 10:16 AM

basketball sheos

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

alibabashoes US

Saturday, March 27, 2010 9:04 AM

alibabashoes

Your blog seems interesting.Regards,Kevin.

china shoes products US

Saturday, March 27, 2010 5:19 PM

china shoes products

This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.

Lebron shoes US

Saturday, March 27, 2010 6:02 PM

Lebron shoes

Your blog is perfect, and I like this article. I find the information I need. I think I can find more useful information here, thanks.

discount designer handbag US

Sunday, March 28, 2010 7:13 AM

discount designer handbag

Amazing article. Bookmarked it already. Best regards, Ken.

nike air force 1 US

Sunday, March 28, 2010 7:57 AM

nike air force 1

You did the a great work writing and revealing the hidden beneficial features of BlogEngine.Net, that I found it was popularly used by bloggers nowadays. I think BE has emerged to be one of the best blogging platform right now. I wish you good luck with your blogging experiences.

nike air max US

Sunday, March 28, 2010 9:01 AM

nike air max

Comfortabl y, the article is in reality the best on this valuable topic. I harmonise with your conclusions and will thirstily look forward to your coming updates. Just saying thanks will not just be sufficient, for the wonderful clarity in your writing. I will instantly grab your rss feed to stay privy of any updates. Fabulous work and much success in your business dealings!

nba stars shoes US

Sunday, March 28, 2010 5:52 PM

nba stars shoes

Funny, I actually had this on my mind a few days ago and now I come across your blog.

nike air jordan US

Wednesday, March 31, 2010 7:17 AM

nike air jordan

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

air max sneaker US

Wednesday, March 31, 2010 8:32 AM

air max sneaker

Comfortabl y, the article is in reality the best on this valuable topic. I harmonise with your conclusions and will thirstily look forward to your coming updates. Just saying thanks will not just be sufficient, for the wonderful clarity in your writing. I will instantly grab your rss feed to stay privy of any updates. Fabulous work and much success in your business dealings!

nike air yeezy US

Saturday, April 03, 2010 7:56 AM

nike air yeezy

Hey, guy, your blog is nice. It can bring me many useful information.

nba shoes sale US

Saturday, April 03, 2010 11:35 AM

nba shoes sale

What a great info, thank you for sharing. this will help me so much in my learning.

discount designer bags US

Saturday, April 03, 2010 4:30 PM

discount designer bags

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.

adidas sneakers US

Saturday, April 03, 2010 5:55 PM

adidas sneakers

Easily, the post is really the greatest on this laudable topic. I concur with your conclusions and will thirstily look forward to your future updates. Saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay privy of any updates. Solid work and much success in your business enterprize!

kobe shoes US

Monday, April 05, 2010 6:26 PM

kobe shoes

This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.

chris shoes US

Wednesday, April 07, 2010 4:29 PM

chris shoes

Easily, the post is really the greatest on this laudable topic. I concur with your conclusions and will thirstily look forward to your future updates. Saying thanks will not just be sufficient, for the fantasti c lucidity in your writing. I will instantly grab your rss feed to stay privy of any updates. Solid work and much success in your business enterprize!

discount handbags sale US

Friday, April 09, 2010 5:22 AM

discount handbags sale

I find your blog in google. And I will be back next time, thanks.

adidas shoes US

Friday, April 09, 2010 6:14 AM

adidas shoes

Your blog is perfect, and I like this article. I find the information I need. I think I can find more useful information here, thanks.

discount designer bags US

Saturday, April 10, 2010 5:51 PM

discount designer bags

Amazing article. Bookmarked it already. Best regards, Ken.

nike air max US

Sunday, April 11, 2010 5:06 PM

nike air max

Substantially, the article is in reality the sweetest on this precious topic. I harmonise with your conclusions and will thirstily look forward to your upcoming updates. Saying thanks will not just be sufficient, for the phenomenal clarity in your writing. I will directly grab your rss feed to stay abreast of any updates. Pleasant work and much success in your business efforts!

wholesale shoes US

Monday, April 12, 2010 6:00 PM

wholesale shoes

Funny, I actually had this on my mind a few days ago and now I come across your blog.

nike shox shoes US

Monday, April 12, 2010 6:03 PM

nike shox shoes

I was thinking of using BlogEngine but then I saw that most of the sites I looked either had comments full of spam or they had simply closed the comments altogether. I hope that you have been able to combat the spam because at the moment it is something that is making me stay away from BE.

nike shox shoes US

Wednesday, April 14, 2010 2:18 PM

nike shox shoes

This is just the information I am finding everywhere. Thanks for your blog, I just subscribe your blog. This is a nice blog.

air force 1s US

Saturday, April 17, 2010 4:53 PM

air force 1s

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.

worldwide shoes china US

Tuesday, April 20, 2010 6:51 PM

worldwide shoes china

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

wholesale shoes sale US

Wednesday, April 28, 2010 5:58 PM

wholesale shoes sale

What a great info, thank you for sharing. this will help me so much in my learning.

kobe shoes US

Wednesday, April 28, 2010 7:04 PM

kobe shoes

Amazing article. Bookmarked it already. Best regards, Ken.

wallets US

Thursday, May 27, 2010 1:41 AM

wallets

What a great info, thank you for sharing. this will help me so much in my learning.

air jordan sale US

Friday, May 28, 2010 10:38 PM

air jordan sale

I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.

discount fendi US

Monday, May 31, 2010 6:44 PM

discount fendi

Thanks for your article.

Adam Credeur us

Tuesday, July 06, 2010 4:01 AM

Adam Credeur

thanks for your shareing, nice artical. www.easyforbuy.com

louis vuitton brand bags US

Tuesday, July 20, 2010 3:36 PM

louis vuitton brand bags

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

designer sandals 2010 US

Wednesday, August 25, 2010 12:15 PM

designer sandals 2010

I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful.

discount gucci US

Friday, September 03, 2010 12:11 AM

discount gucci

I thought it was going to be some boring old post, but it really compensated for my time. I will post a link to this page on my blog. I am sure my visitors will find that very useful.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

Saturday, September 04, 2010 1:40 AM