Mar*_*age 3 data-binding silverlight wpf listbox
我无法使用数据绑定ItemContainerStyle了ListBox在Silverlight 3.它工作正常WPF.这是举例说明我的问题.我真正想要的是绑定到IsSelected属性,但我认为这个例子更容易遵循.
我有一个ListBox绑定到ObservableCollection<Item>的Item对象:
public class Item {
public String Name { get; }
public Brush Color { get; }
}
Run Code Online (Sandbox Code Playgroud)
这是相关的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>
Run Code Online (Sandbox Code Playgroud)
如果TargetType="ListBoxItem"替换为WPF,则可以在WPF中使用相同的XAML 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;
Run Code Online (Sandbox Code Playgroud)
如果我尝试运行这个,我得到一个ArgumentException我的Silverlight控件初始化后.
我究竟做错了什么?如何将属性绑定ItemContainerStyle到项目的属性?
AFAIK Silverlight(甚至3)不支持样式设置器上的绑定.您必须执行一些自定义逻辑来在加载每个项目时更改背景颜色 - 可能是通过将其父项放在可视树中(可能是容器)并将其设置在那里.
| 归档时间: |
|
| 查看次数: |
4152 次 |
| 最近记录: |