如何以编程方式在 WPF 中的现有 ItemContainerStyle 中添加新的 setter?
我有列表视图的项目容器样式,如下所示:
<ListView.ItemContainerStyle>
<Style>
<Setter Property="Padding" Value="0"></Setter>
<Setter Property="Margin" Value="0,0,0,-1"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=EventType}" Value="2">
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="White"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
现在我需要根据项目数据以编程方式为上下文菜单添加新的设置器。我该怎么办?
请指导我......
谢谢
I have item container style for list view like below :
<ListView.ItemContainerStyle>
<Style>
<Setter Property="Padding" Value="0"></Setter>
<Setter Property="Margin" Value="0,0,0,-1"></Setter>
<Style.Triggers>
<DataTrigger Binding="{Binding Path=EventType}" Value="2">
<Setter Property="Background" Value="Green"/>
<Setter Property="Foreground" Value="White"></Setter>
</DataTrigger>
</Style.Triggers>
</Style>
</ListView.ItemContainerStyle>
Now I need to add new setter for context menu programatically based on item data. How can I do it ?
Please guide me.....
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我必须动态显示图像,并根据使用 Style 和 DataTrigger 的复选框的状态显示/隐藏。
规范修改后,我必须动态创建复选框,然后设置样式。
一开始,我有同样的错误消息。
解决方案是创建一个样式对象并将其影响到 Image.Style
希望对您有所帮助
PS:这是我的第一篇文章
I have to display Images dynamically and show/hide depends on state of checkboxes using Style and DataTrigger.
After spec modification i have to create checkboxes dynamically, then to set Style.
At beginning, I have the same error message.
The solution is to create a style object and Affect it to Image.Style
Hope that helping you
PS : It's my first post
我已经实现了以现有样式动态添加设置器的逻辑,如下所示:
但它给了我以下错误:
“‘SetterBaseCollection’在使用(密封)后,就无法修改。”
我认为不可能以密封方式添加新的设置器。所以我有另一个临时解决方案,例如将上下文菜单分配给整个列表视图,而不是预览鼠标右键单击事件上的项目。因此,所有项目的上下文菜单都将相同,然后根据所选项目的数据在编码中进行管理。
I have implemented logic to add setter dynamically in existing style like below :
But it gives me following error :
"After a 'SetterBaseCollection' is in use (sealed), it cannot be modified."
I think it is not possible to add new setter in sealed style. So I have got another temporary solution like assign context menu to whole listview rather than it's item on preview mouse right click event. So context menu will be same for all items then managed in coding based on data of selected item.
如果您创建一个新的 Style() 并“重新添加”设置器,并且使用 Telerik,则可以解决此问题。
If you create a new Style() and "re-add" the the setters, you can get around this issue if you are using Telerik.