- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好吧...这让我很困惑。我已经在我的 UserControl
子类中重写了 OnContentTemplateChanged
。我正在检查为 newContentTemplate
传入的值实际上等于 this.ContentTemplate
(它确实如此),但当我调用此...
var textBox = this.ContentTemplate.FindName("EditTextBox", this);
...它抛出以下异常...
"This operation is valid only on elements that have this template applied."
根据另一个相关问题中的评论者,他说你应该传递控件的内容呈现器,而不是控件本身,所以我然后尝试了这个......
var cp = FindVisualChild<ContentPresenter>(this);
var textBox = this.ContentTemplate.FindName("EditTextBox", cp);
...其中 FindVisualChild
只是 MSDN 示例(见下文)中使用的辅助函数,用于查找关联的内容呈现器。当找到 cp 时,它也会抛出相同的错误。我被难住了!!
这是供引用的辅助函数...
private TChildItem FindVisualChild<TChildItem>(DependencyObject obj)
where TChildItem : DependencyObject {
for(int i = 0 ; i < VisualTreeHelper.GetChildrenCount(obj) ; i++) {
var child = VisualTreeHelper.GetChild(obj, i);
if(child is TChildItem typedChild) {
return typedChild;
}
else {
var childOfChild = FindVisualChild<TChildItem>(child);
if(childOfChild != null)
return childOfChild;
}
}
return null;
}
最佳答案
在调用 FindName
方法之前显式应用模板将防止出现此错误。
this.ApplyTemplate();
关于wpf - 为什么 'this.ContentTemplate.FindName' 会在其自己的模板上抛出 InvalidOperationException ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5679648/
我覆盖了 ResourceDictionary Generic.xaml 中的控件模板。在那里我添加了一个按钮,我想在上面添加一些事件。 --Added my button
在 WPF 应用程序中,如果在 XAML 中声明了 ContentControl, 然后我可以使用 FindName 在代码中轻松引用它: ContentControl cc = Fin
我有一个窗口的 xaml,里面有一个未命名的边框控件。边框控件使用我编写的附加属性,附加属性基本上将边框控件的 Name 属性设置为已知值,例如“占位符”。加载窗口后,它会尝试通过附加属性赋予它的名称
我正在为学校编写一个简单的井字游戏。作业是用 C++ 编写的,但老师允许我使用 C# 和 WPF 作为挑战。我已经完成了所有的游戏逻辑并且表单也基本完成了,但是我遇到了困难。我目前正在使用 Label
为什么 FindName() 在以下示例中返回 null? XAML:
我创建了一个应用了 XAML 模板的自定义控件。在自定义控件中,我需要操作图像。为此,我尝试使用 FindName 在 OnApplyTemplate 中查找图像。但是,FindName 返回 nul
逐渐学会喜欢mono touch 是否有与 FindName 函数等效的函数,以便我可以从其“字符串”名称操作控件 最佳答案 UIKit 控件( View )没有名称,但它们有标签,标签是整数值 您可
在父控件中查找控件的 FramworkElement.FindName() 方法似乎应该直截了当... 但我正在升级,看起来框架不喜欢我正在尝试做的事情。 首先,我确实意识到有很多不同的做事方式,请记
模板
FindName 对我来说是坏的:( 我要找的对象就在那里。我有证据。 场景如下: ToggleButton button = (ToggleButton)sender; Popup popup =
我遇到了一个问题,我使用 FrameworkElement 对象的“FindName()”方法来搜索该元素的子控件。 我注意到了一些有趣的行为,但似乎无法弄清楚。 如果用户滚动浏览器窗口以便控件本身不
我看到我可以访问 ComboBox 的模板化部分( TextBox 、 PopUp 和 Button )通过 FindName方法。 TextBox应该可以使用 cb.FindName("PART_E
根据 C# 编译器和 Silverlight 2 文档,Silverlight 没有为 DataTemplate 类提供 FindName 方法。我想找到一个位于 ContentPresenter 内
我刚刚测试了来自 here 的早期 PowerShell WPF 示例 #requires -version 2 Add-Type -AssemblyName PresentationFramewor
好吧...这让我很困惑。我已经在我的 UserControl 子类中重写了 OnContentTemplateChanged 。我正在检查为 newContentTemplate 传入的值实际上等于
我目前有一个 WPF 项目,它有一个主窗口和许多作为该窗口子级的用户控件。此窗口的许多子项都是选项卡。我已经成功地用一个用户控件替换了我的主窗口,它实现了与主窗口几乎完全相同的功能。 用 UserCo
所以在下面的示例代码中,我创建了一个 UserControl UserControldChild,它是主窗口 Window1.xaml 的子项。为什么 FindName() 方法无法在下面的代码中找到
我是一名优秀的程序员,十分优秀!