I cannot use data binding in ItemContainerStylefor ListBoxin Silverlight 3. It works fine in WPF. This is a contrived example to demonstrate my problem. I really want to bind to a property IsSelected, but I think this example is easier to execute.
I have an object ListBoxrelated :ObservableCollection<Item>Item
public class Item {
public String Name { get; }
public Brush Color { get; }
}
Here is the corresponding Silverlight XAML:
<ListBox x:Name="listBox" ItemsSource="{Binding .}">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="{Binding Color}"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
The same XAML can be used in WPF if TargetType="ListBoxItem"replaced by TargetType="{x:Type ListBoxItem}".
WPF Color Item. Silverlight , XamlParseException AG_E_RUNTIME_MANAGED_UNKNOWN_ERROR. , XAML, , , :
Binding binding = new Binding("Color");
Setter setter = new Setter(ListBoxItem.BackgroundProperty, binding);
Style style = new Style(typeof(ListBoxItem));
style.Setters.Add(setter);
listBox.ItemContainerStyle = style;
, ArgumentException Silverlight.
? ItemContainerStyle ?