WPF 复选框不会显示 UIElement 作为其内容
我正在使用自定义复选框,并尝试将内容设置为 WPF 视图框。问题很简单,什么也没有显示。该复选框出现并起作用,但没有内容。这是我想要做的示例代码(不是实际代码)。
MyCheckbox myCheckbox = new MyCheckbox();
Viewbox viewbox = new Viewbox();
viewbox.Height = 20;
viewbox.Width = 20;
Label label = new Label();
label.Content = "Test";
viewbox.Child = label;
myCheckbox.Content = viewbox;
Content 属性接受一个对象,因此将其设置为 Viewbox 没有问题,并且 ContentPresenter 可以显示 UIElement。我认为 ContentPresenter 可能无法显示 Viewbox 中包含的 Label,因此我创建了一些测试代码,将复选框的内容设置为简单的矩形。还是什么都没有。
有什么想法或想法吗?感谢您的帮助!
哦 - 将 Content 属性设置为字符串是可行的。
I'm working with a custom checkbox, and I'm trying to set the content to a WPF Viewbox. The problem is, simply, nothing is displayed. The checkbox appears and works, but it has no content. This is sample code (not the actual code) of what I'm trying to do.
MyCheckbox myCheckbox = new MyCheckbox();
Viewbox viewbox = new Viewbox();
viewbox.Height = 20;
viewbox.Width = 20;
Label label = new Label();
label.Content = "Test";
viewbox.Child = label;
myCheckbox.Content = viewbox;
The Content property accepts an object, so there is no problem setting it to a Viewbox, and the ContentPresenter can display a UIElement. I thought perhaps the ContentPresenter couldn't display the Label contained in the Viewbox, so I created some test code, setting the checkbox's content to a simple rectange. Still nothing.
Any thoughts or ideas? Thanks for your help!
Oh - Setting the Content property to a string works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要发布自定义
CheckBox
的相关 XAML。检查ControlTemplate
是否有ContentPresenter
,如果您有ContentTemplate
,则它不会干扰。您可以在此处查看示例模板。
如果您只想更改
CheckBox
的外观,则可以重新模板化标准CheckBox
,而无需编写自定义类。You may need to post the relevant XAML of your custom
CheckBox
. Check theControlTemplate
has aContentPresenter
and that if you have aContentTemplate
that it is not interfering.You can see an example template here.
If you just want to change the look of a
CheckBox
, you can re-template the standardCheckBox
without writing a custom class.