I
I
Ignatiy22019-11-07 11:10:34
WPF
Ignatiy2, 2019-11-07 11:10:34

How to call the same context menu for each element with the same type?

I know how to call the context menu for a single element:

<Button Content="12345">
            <Button.ContextMenu>
                <ContextMenu Name="cm">
                    <MenuItem Header="File"/>
                    <MenuItem Header="Save"/>
                    <MenuItem Header="SaveAs"/>
                    <MenuItem Header="Recent Files">
                        <MenuItem Header="ReadMe.txt"/>
                        <MenuItem Header="Schedule.xls"/>
                    </MenuItem>
                </ContextMenu>
            </Button.ContextMenu>
</Button>

But what if I want to set one contextual one for all buttons at once?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Space Purr, 2019-11-07
@Ignatiy2

Perhaps this option will work for you:

<Window.Resources>
    <ContextMenu x:Key="MyContextMenu">
        <!--Ваше меню -->
    </ContextMenu>
</Window.Resources>

<Grid>
     <!-- колонки строки -->
    <TextBox Tag="1" Grid.Column="1" Grid.Row="5" ContextMenu="{StaticResource MyContextMenu}"/>        
    <TextBox Tag="2" Grid.Column="2" Grid.Row="5" ContextMenu="{StaticResource MyContextMenu}"/>                   
</Grid>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question