Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 如何将ContextMenu放入Window.Resources_Wpf_Xaml - Fatal编程技术网

Wpf 如何将ContextMenu放入Window.Resources

Wpf 如何将ContextMenu放入Window.Resources,wpf,xaml,Wpf,Xaml,如您所见,我可以将高度属性和前景属性放入窗口。参考资料 <Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="52

如您所见,我可以将高度属性和前景属性放入窗口。参考资料

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">

<Window.Resources>
    <Style TargetType="{x:Type TextBox}">
        <Setter Property="Height" Value="50"/>
        <Setter Property="Foreground" Value="Green"/>
    </Style>
</Window.Resources>

<Grid>
    <TextBox Text="StackOverFlow">
        <TextBox.ContextMenu>
            <ContextMenu Background="Blue"/>
        </TextBox.ContextMenu>
    </TextBox>
</Grid>

</Window>


如何将
放入窗口。资源?

您可以将上下文菜单放入单独的资源中

<Window.Resources>
    <ContextMenu x:Key="ContextMenu" Background="Blue"/>
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Height" Value="50"/>
        <Setter Property="Foreground" Value="Green"/>
        <Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/>
    </Style>
</Window.Resources>

<Grid>
    <TextBox Text="StackOverFlow"/>
</Grid>

您可以将上下文菜单放入单独的资源中

<Window.Resources>
    <ContextMenu x:Key="ContextMenu" Background="Blue"/>
    <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Height" Value="50"/>
        <Setter Property="Foreground" Value="Green"/>
        <Setter Property="ContextMenu" Value="{StaticResource ContextMenu}"/>
    </Style>
</Window.Resources>

<Grid>
    <TextBox Text="StackOverFlow"/>
</Grid>


亲爱的Pavel Anikhouski,您能告诉我为什么要输入以下代码吗
BasedOn=“{StaticResource{x:Type TextBox}}”
这意味着您的样式基于默认的
TextBox
样式。您可以查看一些示例。我删除了这一行,代码仍然有效。那么这真的有必要吗?这在更复杂的场景和样式定制中是必要的Dar Pavel Anikhouski,你能告诉我为什么要使用下面的代码吗
BasedOn=“{StaticResource{x:Type TextBox}}”
这意味着您的样式基于默认的
TextBox
样式。您可以查看一些示例。我删除了这一行,代码仍然有效。那么这真的有必要吗?在更复杂的场景和样式定制中是必要的