本文介绍了在代码隐藏中找不到XAML控件(Control.Template.FindName)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有这个UserControl: $
So I have this UserControl:
<UserControl x:Class="DinerPOS.Restaurant.Windows.UserMenuInterface" xmlns="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/winfx/2006/xaml" xmlns:mc="schemas.openxmlformats/markup-compatibility/2006" xmlns:d="schemas.microsoft/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>
我想要做的是访问代码隐藏中的BeverageMenuItem控件,但它永远找不到。
What I want to do is access the BeverageMenuItem control in the code-behind, but it's never found.
代码落后:
CustomMenuItem BeverageMenuItem = (CustomMenuItem)MenuImageContextMenu.Template.FindName("BeverageMenuItem", MenuImage);如何访问此控件?
How do I access this control?
推荐答案
由于上下文菜单很可能无法打开,因此无法创建BeverageMenuItem,因此无法找到。你到底想要完成什么? Because the context menu is most likely not open BeverageMenuItem is not created so cannot be found. What exactly are you trying to accomplish?
更多推荐
在代码隐藏中找不到XAML控件(Control.Template.FindName)
发布评论