XAML 在代码隐藏中找不到控件 (Control.Template.FindName)
XAML control not found in code-behind (Control.Template.FindName)
我试图在后面的代码中访问 XAML 控件(CustomMenuItem 控件,BeverageMenuItem
),但它 returns 为 Null
。
<UserControl x:Class="DinerPOS.Restaurant.Windows.UserMenuInterface"
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"
xmlns:customcontrols="clr-namespace:System.Windows.WPF.Controls;assembly=CustomControls"
xmlns:resources="clr-namespace:DinerPOS.Properties"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Image x:Name="MenuImage" Grid.Column="1" Grid.Row="1" Cursor="/DinerPOS;component/Resources/Cursors/Hand.cur"
Source="/DinerPOS;component/Resources/Images/Restaurant/Beverages/Beverage.png" Stretch="Fill">
<Image.ContextMenu>
<ContextMenu x:Name="MenuImageContextMenu" Background="White" Cursor="/DinerPOS;component/Resources/Cursors/Hand.cur" Width="175" Height="100">
<ContextMenu.Template>
<ControlTemplate x:Name="MenuImageTemplate">
<Grid x:Name="ContextMenuGrid" Background="{TemplateBinding Background}">
<customcontrols:CustomMenuItem x:Name="BeverageMenuItem" />
</Grid>
</ControlTemplate>
</ContextMenu.Template>
</ContextMenu>
</Image.ContextMenu>
</Image>
</UserControl>
代码在UserMenuInterface.xaml.cs
后面
CustomMenuItem BeverageMenuItem = (CustomMenuItem)MenuImageContextMenu.Template.FindName("BeverageMenuItem", MenuImage);
您要搜索的控件是在模板中定义的。必须先实例化该模板,然后才能搜索该模板中包含的控件。这是在引发模板化控件的 Loaded
事件时发生的,在您的情况下,这发生在打开上下文菜单时。
UserMenuInterface 的代码隐藏:
public UserMenuInterface()
{
InitializeComponent();
this.MenuImageContextMenu.Loaded += FindControl;
}
private void FindControl(object sender, RoutedEventArgs e)
{
var BeverageMenuItem = this.MenuImageContextMenu.Template.FindName("BeverageMenuItem", MenuImage) as CustomMenuItem;
}
我试图在后面的代码中访问 XAML 控件(CustomMenuItem 控件,BeverageMenuItem
),但它 returns 为 Null
。
<UserControl x:Class="DinerPOS.Restaurant.Windows.UserMenuInterface"
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"
xmlns:customcontrols="clr-namespace:System.Windows.WPF.Controls;assembly=CustomControls"
xmlns:resources="clr-namespace:DinerPOS.Properties"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<Image x:Name="MenuImage" Grid.Column="1" Grid.Row="1" Cursor="/DinerPOS;component/Resources/Cursors/Hand.cur"
Source="/DinerPOS;component/Resources/Images/Restaurant/Beverages/Beverage.png" Stretch="Fill">
<Image.ContextMenu>
<ContextMenu x:Name="MenuImageContextMenu" Background="White" Cursor="/DinerPOS;component/Resources/Cursors/Hand.cur" Width="175" Height="100">
<ContextMenu.Template>
<ControlTemplate x:Name="MenuImageTemplate">
<Grid x:Name="ContextMenuGrid" Background="{TemplateBinding Background}">
<customcontrols:CustomMenuItem x:Name="BeverageMenuItem" />
</Grid>
</ControlTemplate>
</ContextMenu.Template>
</ContextMenu>
</Image.ContextMenu>
</Image>
</UserControl>
代码在UserMenuInterface.xaml.cs
后面 CustomMenuItem BeverageMenuItem = (CustomMenuItem)MenuImageContextMenu.Template.FindName("BeverageMenuItem", MenuImage);
您要搜索的控件是在模板中定义的。必须先实例化该模板,然后才能搜索该模板中包含的控件。这是在引发模板化控件的 Loaded
事件时发生的,在您的情况下,这发生在打开上下文菜单时。
UserMenuInterface 的代码隐藏:
public UserMenuInterface()
{
InitializeComponent();
this.MenuImageContextMenu.Loaded += FindControl;
}
private void FindControl(object sender, RoutedEventArgs e)
{
var BeverageMenuItem = this.MenuImageContextMenu.Template.FindName("BeverageMenuItem", MenuImage) as CustomMenuItem;
}