【发布时间】:2015-04-21 15:43:27
【问题描述】:
我正在尝试将我的元素绑定到字典中定义的数据模板中。 让我们变得简单。
我有一个简单的课程
public class A { public string Data {get;set} }
我有一个包含 ListBox 的简单视图,其中 ItemSources 是 A 类的列表:
<ListBox ItemsSource="{Binding AList}">
关键是,当我直接在视图中定义 Itemplate 时,绑定有效:
<ListBox.ItemTemplate>
<DataTemplate >
<TextBlock Text="{Binding Data}" />
<Rectangle Fill="Red" Height="10" Width="10"/>
</DataTemplate>
</ListBox.ItemTemplate>
这很好用。
但是当我在资源字典中定义这个 ItemTemplate 时,绑定不起作用?
我该怎么做?
PS:这是一个简单的例子来解释我的问题,不要告诉我重写 toString 函数以使其工作或使用类模板,我的实际情况比这要复杂得多。
感谢您的帮助
【问题讨论】: