See Sharp
Is it really just a matter of point of view ?

This silver is kinda light on the bindings : you can't do them with style !

February 2, 2010 14:00 by salfab

Depuis peu, voilà qu'il m'est donné la possibilité de jouer quelque peu avec Silverlight :

Beaucoup de choses disponibles en WPF ne le sont pas en Silverlight. Habituellement, des choses facilement contournables, mais la petite aventure du jour mérite certainement d'être consignée : 

Silverlight en tous cas jusqu'à la version 3 ne supporte pas le databinding dans les style setters.

Voyons un cas dans lequel ceci peut être embêtant :

Imaginons une ListBox dont on voudrait remplacer le pannel par défaut par un Canvas, de façon à y positionner les Items. Leur position serait définie par des coordonnées stockées dans les éléments eux-même, et exposées par une DependencyProperty.

Malheureusement, le contrôle ListBox encapsule ses éléments dans un ListBoxItem. Si l'on modifie le ItemTemplate pour ajouter les propriétés attachées Canvas.Top et Canvas.Left à l'élément racine du DataTemplate, l'effet ne sera pas appliqué.

Ceci s'explique par le fait que l'élément "racine" du DataTemplate ne sera pas directement dans le canvas : il sera contenu dans un ListBoxItem. Il faut donc définir les propriétés attachées Canvas.Left et Canvas.Top dans le ListBoxItem.

Ceci est faisable grâce à la propriété ItemContainerStyle qui contient le style du ListBoxItem.

<ListBox.ItemContainerStyle>
   
<Style>
       
<Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
        
<Setter Property="Canvas.Left" Value="{Binding Path=X}" />
   
</Style>
</ListBox.ItemContainerStyle>

En WPF, ceci ne pose pas de problème, on peut définir avec un Style Setter, les propriétés attachées du Canvas, en leur appliquant un Binding.

En Silverlight, malheureusement, s'il est effectivement possible de donner des valeurs fixes pour les propriétés attachées du Canvas, il n'est pas possible de procéder de la sorte pour récupérer par Binding les coordonnées auxquelles placer l'élément : comme le DataBinding n'est pas supporté dans les style setter, il faudra trouver un moyen de créer ce binding dans le codebehind.

Ceci sera faisable en sous-classant le contrôle ListBox et en créant un override de la méthode PrepareContainerForItemOverride(DependencyObject element, object item), dans laquelle il sera possible de définir le Binding par code :    

        FrameworkElement contentitem = element as FrameworkElement; 
       
Binding leftBinding = new Binding("Left"); // "Left" is the property path that you want to bind the value to. 
        contentitem
.SetBinding(Canvas.LeftProperty, leftBinding); 
 
       
base.PrepareContainerForItemOverride(element, item); 

À noter que ItemsControl encapsule également ses éléments dans un ItemContainer, mais ItemContainerStyle n'est pas exposé par ItemsControl en Silverlight...


Comments

April 4. 2010 13:37

games addicting

Considerably, the blog post is genuinely the greatest on this laudable matter. I come to an understanding with your results and will desperately look forward to your future updates. Just saying thankx will not likely just be sufficient, for the fantasti c clearness in your writing. I will right away look for your feeds to keep privy of any updates. Gratifying work and much success in your business dealings. All the best, !

games addicting

April 14. 2010 07:25

romeo michigan

This is my first visit here, and just wanted to say I enjoy your site.
I'll be back for sure!

romeo michigan

May 4. 2010 23:41

roseville michigan newspaper

Thank you for taking the time to discuss this, I feel strongly about it and like learning more on this subject.
If as you can, as you gain expertise, would you mind updating your site with extra information? It is extremely useful for me.

roseville michigan newspaper

May 8. 2010 15:30

Mariana

I have a nice joke.  Why did the big moron fall off the roof and the little moron didn't? Because he was a little more on.

Mariana

May 10. 2010 04:04

fg

hi..

fg

May 11. 2010 06:26

Cost To Start a Tanning Salon

Yeah but there some instances that Silverlight has its own specialty, or it still has some best qualities that give a better performance.

Cost To Start a Tanning Salon

May 13. 2010 11:26

baby info

I did a study on this type of thing 3 years ago, great topic.

baby info

May 13. 2010 11:44

Hotel U Leva in Slovakia

It’s always nice to see your post with lots of good stuff and nice ideas to enjoy. Thanks for sharing valuable post like this. Cheers! Laughing

Hotel U Leva in Slovakia

May 20. 2010 00:52

Wahdo

After reading the article, I feel that I need more information on the topic. Can you suggest some resources please?

Wahdo

May 21. 2010 03:36

Creative Writing Jobs

It’s always nice to see your post with lots of good stuff and nice ideas to enjoy. Thanks for sharing valuable post like this. Cheers! Laughing

Creative Writing Jobs

May 27. 2010 11:50

saving

Great post, Information about these Issues Always seem to make my day better!

saving

June 1. 2010 08:22

hidayat

i really appreciate this your good article
from there i get something that i want to know
thanks for this usefull informations

hidayat

June 21. 2010 18:08

seo wilmington

I was very delighted to locate this site on bing.I wished to say thank you to you with regard to this wonderful post!! I definitelyenjoyed every little bit of it and I've you bookmarked to check out new stuff you post.

seo wilmington

June 22. 2010 06:09

explore talent reviews

Hello this is a good write-up. I'm going to e-mail this to my buddies. I came on this while exploring on yahoo I'll be sure to come back. thanks for sharing.

explore talent reviews

June 24. 2010 01:33

email archive

Your article continually include a decent amount of really up to date info.  Where do you come up with this?  Just declaring you are very innovative.  Thanks again

email archive

June 24. 2010 06:56

poptropica cheats

One of the more impressive blogs Ive seen.  Thanks so much for keeping the internet classy for a change.  Youve got style, class, bravado.  I mean it.  Please keep it up because without the internet is definitely lacking in intelligence.

poptropica cheats

July 2. 2010 07:52

abercrombie

Site truly interesting. Congratulations!

abercrombie

July 2. 2010 21:21

Elmer Blohm

I found a link to your site on a Currency trading blog, and I should say... Your web site is a lot better. You make the subject easier to understand, thanks

Elmer Blohm

July 3. 2010 04:48

Nan Wauson

Wonderful article, thanks. Could you clarify the third point in additional detail please?

Nan Wauson

July 3. 2010 16:53

Kurtis Meszaros

I used to have a site on this subject, but I had so many spam comments I had to close it. You appear to be doing a better job keeping out the spammers! Kudos!

Kurtis Meszaros

July 6. 2010 04:12

Nell Halliwell

I found a link to your blog on a Currency trading site, and I should say... Your website is a lot better. I understand it better now, thanks

Nell Halliwell

July 13. 2010 21:24

Horace Faulcon

This is fantastic! How did you learn the subject when you were first getting into it?

Horace Faulcon

July 17. 2010 16:44

cheap airline tickets

I saw something similar to this on a different web site and didn't really understand it, but this post helped me understand it better. Thanks!

cheap airline tickets

July 24. 2010 01:03

kenosha flat fee mls

Great Blog I love the lay out and the color scheme is it possible to get a copy of your theme?

kenosha flat fee mls

July 27. 2010 23:15

migraine

ick the link above for today's Coupons, Instant Rebates and Free Shipping offers at Amazon Grocery. Amazon Gift Cards Amazon Kindle Review

migraine

July 28. 2010 20:02

PowWeb Review

Cheers for this article, It's fantastic to see another BlogEngine.NET user. Most users these days tend to use other systems like Wordpress, but I think BlogEngine is the better system to use.

PowWeb Review

August 12. 2010 09:38

Travel_sports

Quick Heads Up! ! http://www.bagsok.com/Travel_sports is Most Spectacular for Sweetie in 2009

Travel_sports

August 13. 2010 04:22

bags

Why Pick http://www.bagsok.com for Housewives

bags

August 14. 2010 06:34

Williams Karpel

Hello I just discovered an excellent new program to make more money from home by writing articles, blogs, manuals, newsletters and more. I get new writing contracts sent to me day by day and they're an extremely helpful and friendly organisation. Whether you might be experienced, or a newbie, it's well worth trying them out. <a href="http://bit.ly/8Z5gaC">Go to this site to find out more!</a>

Williams Karpel

August 17. 2010 10:33

mysiteinfo

8 Easy Tricks to Buy http://www.mysiteinfo.org/ Get Ready Fast – Quick and Easy Ways for http://www.mysiteinfo.org/ Some Details to Take Care of Your http://www.mysiteinfo.org/

mysiteinfo

August 17. 2010 18:11

buy leica M8

I watched a program concerning that on television the other night. Thanks for explaining it more thoroughly

buy leica M8

August 18. 2010 02:52

Cross-body_bags

Healthy Tutorials to Take Care of http://www.bagsok.com Easter Ideas – Gifts, Activities, and http://www.bagsok.com/Cross-body_bags Wardrobe Essentials for Fianceé

Cross-body_bags

August 20. 2010 16:40

Merrill Shoes

Thank you for this article, It's a interesting read which was very helpful.

Merrill Shoes

August 27. 2010 05:36

girls cam

Awesome post I bookmared it on Delicious and submitted on Digg. Hopefully it sends more traffic your way

girls cam

August 30. 2010 03:06

Best Smokeless Cigarette

Thanks for the informative article. Will check out more of your blog posts!

Best Smokeless Cigarette

September 1. 2010 02:29

Master Cleanse Lemonade Diet

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

Master Cleanse Lemonade Diet

September 5. 2010 05:19

cam sex

adding this info to my blog if thats ok dude.

cam sex

September 8. 2010 14:05

ysl shoes

like it

ysl shoes

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading