Agata
Agata

Reputation: 131

Silverlight 4: Is it possible to bind ListBoxItem's width and height property?

I've got following issue:

I've got a ListBox which items are lain on Canvas. I would like to be able to xamly position its elements. The ItemContainerStyle looks like this:

<Style x:Key="ElementContainerStyle" TargetType="ListBoxItem" >
  <Setter Property="Canvas.Top" Value="{Binding BoundingBox.Y}" />
  <Setter Property="Canvas.Left" Value="{Binding BoundingBox.X}" />
  <Setter Property="Width" Value="{Binding BoundingBox.Width}" />
  <Setter Property="Height" Value="{Binding BoundingBox.Height}" />
  ...
</Style>

With such a style SL crashes with InnerException's message:

System.NotSupportedException: Cannot set read-only property

Why's that read-only property? If I put regular values there (not data bounded) it runs well, except - this is not what I wanted.

I was looking forward to new features for SL4 (like ability to bind to Width and Height properties), but it seems that such a trick still can't be done?

It works fine in WPF..

Anyone can shed some light on this?

Upvotes: 0

Views: 694

Answers (1)

CodeNaked
CodeNaked

Reputation: 41393

Assigning a Binding to the Setter.Value property is not supported in Silverlight. Check out this blog post for a workaround, which uses an attached property to create/assign the binding when the Style is applied to an instance.

Upvotes: 1

Related Questions