在代码隐藏中找不到XAML控件(Control.Template.FindName)

编程入门 行业动态 更新时间:2024-10-14 06:21:12
本文介绍了在代码隐藏中找不到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)

本文发布于:2023-11-24 12:38:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1625235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   中找   代码   Template   FindName

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!