简体   繁体   中英

How to set a ToolTip and ContextMenu assigned to all controls defined in ListView.ItemTemplate

I am using my own definition of ItemTemplate

<ListView.ItemTemplate>
    <DataTemplate>
        <StackPanel>
        ...
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>

The stack panel ownes several contols. The purpose is to popup the same ContextMenu if the user right clicks on any control of the stack panel and display the same tooltip when a mouse is over a control.

Is there a possibility to do the tooltip/contextmenu assignment in a single place rather that to do this individually for each control?

Is there a possibility to do the tooltip/contextmenu assignment in a single place rather that to do this individually for each control?

Set the ContextMenu/Tooltip property of the StackPanel and its value will be inherited by the child controls, ie you will see the same ContextMenu/Tooltip when you right-click on or hover over any of the StackPanel's child controls:

<ListView x:Name="lv">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <StackPanel.ContextMenu>
                    <ContextMenu>
                        <MenuItem Header="..." />
                    </ContextMenu>
                </StackPanel.ContextMenu>
                <StackPanel.ToolTip>
                    <ToolTip>
                        <TextBlock>Tooltip...</TextBlock>
                    </ToolTip>
                </StackPanel.ToolTip>
                <Button Content="Button"/>
                <TextBlock Text="..." />
                <ComboBox>
                    <ComboBoxItem>1</ComboBoxItem>
                    <ComboBoxItem>2</ComboBoxItem>
                    <ComboBoxItem>3</ComboBoxItem>
                </ComboBox>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM