[英]calling user control to the mainwindow in wpf
此问题与如何使用上下文菜单有关,该菜单是在主窗口中作为用户控件创建的。 用户控件就像..
<UserControl x:Class="contextmenu1.context"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300" >
<UserControl.Resources>
<ContextMenu x:Key="conmen" >
<MenuItem Header="Cut" Foreground="Black" FontSize="14" >
<!--<MenuItem.Icon >
<Image Source="Images/cut.png" Height="20" Width="20" />
</MenuItem.Icon>-->
</MenuItem>
<Separator />
<MenuItem Header="Copy" Foreground="Black" FontSize="14">
<!--<MenuItem.Icon>
<Image Source="Images/copy.png" />
</MenuItem.Icon>-->
</MenuItem>
<Separator />
<MenuItem Header="Paste" Foreground="Black" FontSize="14">
<!--<MenuItem.Icon>
<Image Source="Images/paste.png" />
</MenuItem.Icon>-->
</MenuItem>
<Separator />
<MenuItem Header="Delete" Foreground="Black" FontSize="14">
</MenuItem>
<Separator />
</ContextMenu>
<!--<Style TargetType="Button">
<Setter Property="ContextMenu" Value="{StaticResource conmen}"/>
</Style>-->
</UserControl.Resources>
</UserControl>
和主窗口的代码就像..
<Window x:Class="contextmenu1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dc="clr-namespace:contextmenu1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button x:Name="conbut" Content="RightClick" HorizontalAlignment="Left" Margin="234,135,0,0"/>
<dc:context />
</Grid>
</Window>
我包括了必需的名称空间,但是以某种方式单击按钮contextmenu时没有出现,感谢您的帮助。
无需为上下文菜单定义用户控件。 而是在公共资源字典中声明它(例如,在App.xaml中)。 然后,您可以通过StaticResource
访问上下文菜单:
在App.xaml中:
<Application.Resources>
<ContextMenu x:Key="contextMenu">
...
</ContextMenu>
</Application.Resources>
在窗口中:
<Button x:Name="conbut" ... ContextMenu="{StaticResource contextMenu}" />
只要在Window Grids ContextMenu
定义它,就不需要该按钮,在Grid空间上单击任何右键都将打开ContextMenu
<Window x:Class="contextmenu1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:dc="clr-namespace:contextmenu1"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ContextMenu>
<ContextMenu x:Key="contextMenu">
...
</ContextMenu>
</Grid.ContextMenu>
</Grid>
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.