WPF Displays or disables the right-click menu for each item in the ListView based on specified criteria

Source: Internet
Author: User

Yesterday to do a ListView right-click menu, you need to according to the ListView binding collection of each object in a bool property to determine whether to display the right-click menu of the item, after unremitting attempts and efforts, finally implemented two scenarios, one is when the value is False, you can click the right-click menu , but the menu item gray is not selectable, the other is not directly out of the menu, right-click No response. Here's the first scenario:

     <ListViewx:name= "ServerList"ItemsSource="{Binding Path=servers}">            <listview.resources>                <Stylex:key= "Checkmenuitem"TargetType="{x:type MenuItem}">                    <style.triggers>                        <DataTriggerBinding="{Binding path=islogin, Converter={staticresource bool2string}}"Value= "True">                            <Setter Property= "IsEnabled"Value= "False"/>                        </DataTrigger>                    </style.triggers>                </Style>                <ContextMenux:key= "Contextmenuitem">                    <MenuItemHeader= "Play with her"Style="{StaticResource Checkmenuitem}"/>                    <MenuItemHeader= "Kiss Her"Style="{StaticResource Checkmenuitem}"/>                </ContextMenu>            </listview.resources>            <Listview.itemcontainerstyle>                <StyleTargetType="{x:type ListViewItem}">                    <Setter Property= "ContextMenu"Value="{StaticResource Contextmenuitem}"/>                </Style>            </Listview.itemcontainerstyle>            <Listview.view>                <GridView>                    <GridviewcolumnHeader= "Server"Width= "485"displaymemberbinding="{Binding ServerName}"/>                </GridView>            </Listview.view>        </ListView>

In this code, the data source servers is a observablecollection<serverviewmodel> type, The variable islogin that is bound in DataTrigger is a bool property in Serverviewmodel that determines whether the right-click menu for an item in the ListView is available.

The second scenario:

 <ListViewx:name= "ListView"HorizontalAlignment= "Left"Height= "+"Margin= "9,82,0,0"VerticalAlignment= "Top"Width= "Auto"ItemsSource="{Binding Path=servers}">         <listview.resources>            <ContextMenux:key= "ct">                  <MenuItemHeader= "Play with her" />                  <MenuItemHeader= "Kiss Her" />            </ContextMenu>         </listview.resources>         <Listview.itemcontainerstyle>            <StyleTargetType="{x:type ListViewItem}">               <Setter Property= "ContextMenu"Value="{StaticResource ct}"></Setter>                    <style.triggers>                        <DataTriggerBinding="{Binding path=islogin, Converter={staticresource bool2string}}"Value= "True">                            <Setter Property= "contextmenuservice.isenabled"Value= "False"/>                        </DataTrigger>                    </style.triggers>            </Style>         </Listview.itemcontainerstyle>                 <Listview.view>            <GridView>               <GridviewcolumnHeader= "header"Width= "485"displaymemberbinding="{Binding ServerName}"/>            </GridView>         </Listview.view>      </ListView>

This method differs from the previous one in that the previous method is changing the state of the item (MenuItem) In the context menu (ContextMenu) According to the condition, which is available or unavailable, and this method determines each item in the ListView according to the condition (ListViewItem) Whether the right-click menu is displayed is to modify the value of contextmenuservice.isenabled.

In addition to share my previous attempts to fail the method, we refer to the first kind:

      <listview.resources>                <Stylex:key= "Checkcontextmenu"TargetType="{x:type ContextMenu}">                       <style.triggers>                          <DataTriggerBinding="{Binding path=islogin, Converter={staticresource bool2string}}"Value= "True">                             <Setter Property= "Visibility"Value= "Collapsed"/>                          </DataTrigger>                       </style.triggers>                 </Style>                <ContextMenux:key= "ct"Style="{StaticResource Checkcontextmenu}">                    <MenuItemHeader= "Play with her" />                    <MenuItemHeader= "Kiss Her" />                </ContextMenu>            </listview.resources>

is to add style to ContextMenu in Resoures, this way comes out with a bug, that is, when you click on the Allow menu to display the item when there is no problem, when you click on the menu is not allowed to display the item will not show the menu, However, when you click on an item that does not allow the menu to be displayed and then click on the item that allows the menu to be displayed, the menu that appears is the one that was originally not allowed to display the menu. This is what caused the cause I did not study.

The second type:

          <Listview.style>                <Style>                    <style.triggers>                        <DataTriggerBinding="{Binding Selecteditem.islogin}"Value= "True">                            <Setter Property= "contextmenuservice.isenabled"Value= "True"/>                        </DataTrigger>                    </style.triggers>                </Style>            </Listview.style>

This is not a viable thing.

OK, write here today, Saturday afternoon to the company to write a blog, finished feeling pretty substantial, time is not wasted, night to see the film Wow Kaka Kaka

WPF Displays or disables the right-click menu for each item in the ListView based on specified criteria

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.