C#-WPF-设计时自定义控件ISES(取二)
在我最初的帖子中,我试图从DockPanel继承一个控件。这最终失败了,所以现在我使用一个裸类重新创建了控件。然而,我遇到了类似的问题。在设计期间,我需要将其他控件放置在自定义控件(当前为网格)的容器中,并能够在设计器中选择和编辑它们 DockPanel.csC#-WPF-设计时自定义控件ISES(取二),c#,wpf,xaml,custom-controls,design-time,C#,Wpf,Xaml,Custom Controls,Design Time,在我最初的帖子中,我试图从DockPanel继承一个控件。这最终失败了,所以现在我使用一个裸类重新创建了控件。然而,我遇到了类似的问题。在设计期间,我需要将其他控件放置在自定义控件(当前为网格)的容器中,并能够在设计器中选择和编辑它们 DockPanel.cs [ContentProperty("OtherContent")] public class DockPanel : ContentControl, INotifyPropertyChanged { /* ..
[ContentProperty("OtherContent")]
public class DockPanel : ContentControl, INotifyPropertyChanged
{
/* ... other properties etc goes above here ... */
private object gContent = new Control();
public object OtherContent
{
get { return gContent; }
set { gContent = value; OnPropertyChanged("OtherContent"); }
}
public DockPanel()
{
//Setup default values for all other controls here..
This.Content = BdrCtrl; // probably should use SetValue() for this.
BdrCtrl.Child = gCtrl; //border that holds the main grid control so I can apply rounded corners.
OtherContent = new Control();
gCtrl.Children.Add(OtherContent as Control);
// gctrl == grid that's a child of a border which is a child of DockPanel
// itself. There's also a header being added for title etc as well.
}
private void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new
PropertyChangedEventArgs(propertyName));
}
}
XAML:
该按钮在设计时或运行时不显示。我不能将任何控件拖到不替换DockPanel.Content的自定义控件上。我错过了什么?我不能在XAML中使用绑定,因为这样就无法将控件拖动到其中
下面是完整的类:什么是
gCtrl
,为什么希望它的子类出现在DockPanel子类中?因此,我在您的代码中没有看到任何试图在任何地方显示OtherContent
的内容。其次,不要对控件使用INotifyPropertyChanged
。对控件使用依赖项属性。这两种属性是不同的。Gctrl是网格容器,它是DockPanel的子级border的子级。哦,哎呀,我忘了把它添加到边界的那一行了。我会更新代码。好的,代码更新了。我将尝试一个依赖性属性并返回给您。将一些随机属性转换为依赖性属性并不能解决您的问题。@EdPlunkett我甚至不知道有wpf工具箱这样的东西:)
<Controls:DockPanel Title="Explorer">
<Button Content="Test"/>
</Controls:DockPanel>