【发布时间】:2014-05-09 08:30:58
【问题描述】:
我在 wpf 中创建自定义控件时遇到问题。我总是打算完全在代码隐藏中创建自定义控件,以防我需要扩展它们。
问题是当我将它们添加到 xaml 中时,它们是可编辑的。我不知道如何解释,但是当我点击它们时,我可以写进去。我认为这是我编写自定义控件的方式。这是一个示例代码。
public class MyCustomControl : UserControl
{
public MyCustomControl()
{
var button = new Button();
this.Content = button; // I always assign the root element to the Content property of the UserControl. I think this is the case
}
}
正如我所说,当我在 Xaml 部分中添加 MyCustomControl 时,它工作得很好,但是当我单击它时(再次在 xaml 部分中)它是可编辑的,我可以写入它。我到底有什么问题?
[编辑]我添加了屏幕截图
这是代码隐藏
public class MyCustomControl : UserControl
{
public MyCustomControl()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.Content = new Grid() { Background = Brushes.Red };
}
}
【问题讨论】:
-
你能提供你的问题的截图吗?
-
当然。我添加了截图@samar。
标签: c# wpf custom-controls