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
样式。您可以查看一些示例。我删除了这一行,代码仍然有效。那么这真的有必要吗?在更复杂的场景和样式定制中是必要的