C# 如何用ListBoxItem的自定义子类填充WPF列表框?
我在WPF中创建了ListBox和ListBoxItem的子类,因为我想向这两个控件添加一些自定义依赖项属性。我用了这个例子: 实现此操作的代码如下所示:C# 如何用ListBoxItem的自定义子类填充WPF列表框?,c#,wpf,dependencies,C#,Wpf,Dependencies,我在WPF中创建了ListBox和ListBoxItem的子类,因为我想向这两个控件添加一些自定义依赖项属性。我用了这个例子: 实现此操作的代码如下所示: protected override DependencyObject GetContainerForItemOverride() { CustomBoxItem container = new CustomBoxItem(); return container; } 这很好
protected override DependencyObject GetContainerForItemOverride()
{
CustomBoxItem container = new CustomBoxItem();
return container;
}
这很好,但我的问题是:我必须手动设置所有依赖项属性吗?因为在我的CustomBox.Xaml中,我无法在根标记中设置依赖项属性。我希望在xaml中执行此操作:
<UserControl mycustomdp="SomeValue">
但是当我想要设置绑定时,它将远不如在XAML中设置属性那么优雅。这有可能吗?听起来好像做错事了。您的自定义ListBoxItem为何如此特殊,以至于无法使用标准ListBoxItem和自定义模板实现相同的功能?我想向ListBoxItem添加依赖项属性什么类型的依赖项属性?为何
container.mycustomdp = "SomeValue";