如何在 WPF 的现有 ItemContainerStyle 中以编程方式添加新的 setter? [英] How to add new setter programatically in existing ItemContainerStyle in WPF?

查看:26
本文介绍了如何在 WPF 的现有 ItemContainerStyle 中以编程方式添加新的 setter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有如下列表视图的项目容器样式:

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>

现在我需要根据项目数据以编程方式为上下文菜单添加新的 setter.我该怎么做?

Now I need to add new setter for context menu programatically based on item data. How can I do it ?

请指导我......

谢谢

推荐答案

我已经实现了在现有样式中动态添加 setter 的逻辑,如下所示:

I have implemented logic to add setter dynamically in existing style like below :

listview1.ItemContainerStyle.Setters.Add(new Setter(Control.ContextMenuProperty,GetItemContextMenu(txtName.Text)));

但它给了我以下错误:'SetterBaseCollection' 使用(密封)后,将无法修改."

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.

这篇关于如何在 WPF 的现有 ItemContainerStyle 中以编程方式添加新的 setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆