Silverlight 列表框可选 ItemTemplate
我有一个包含 ListBox (lstClients) 和 ComboBox 的用户控件
ListBox 有 2 个 DataTemplates 设置为名为“LowDetailTemplate”和“HighDetailTemplate”的资源
当我更改 ComboBox 中的值时,我需要能够在 2 个 DataTemplates 之间切换从“低”到“高”,反之亦然。在 ComboBox 的 SelectionChanged 事件中,我猜我需要更改 ListBox 的 ItemTemplate,但我正在努力处理在代码后面分配 DataTemplate 的代码。我的最新尝试如下所示,但在运行时失败。
lstClients.ItemTemplate = (DataTemplate)this.Resources["LowDetailTemplate"];
I have a Usercontrol that contains a ListBox (lstClients) and a ComboBox
The ListBox has 2 DataTemplates setup as Resources called "LowDetailTemplate" and "HighDetailTemplate"
I need to be able to switch between the 2 DataTemplates when I change the value in a ComboBox from "Low" to "High" and vice versa. In the SelectionChanged event of the ComboBox I'm guessing I need to change the ItemTemplate of the ListBox but I'm struggling with the code to assign the DataTemplate in code behind. My latest attempt is shown below but it fails at runtime.
lstClients.ItemTemplate = (DataTemplate)this.Resources["LowDetailTemplate"];
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在同一数据模板中定义两个视图,然后使用简单的变量切换可见视图会更容易。然后,您的组合框更改只会更新该简单变量,从而导致每个模板实例显示的视图发生更改。
It would be easier to define both of the views inside the same data template and then switch which is visible by use of a simple variable. Then your combo box change would just update that simple variable and so cause the view shown for each template instance to change.