Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何用ListBoxItem的自定义子类填充WPF列表框?_C#_Wpf_Dependencies - Fatal编程技术网

C# 如何用ListBoxItem的自定义子类填充WPF列表框?

C# 如何用ListBoxItem的自定义子类填充WPF列表框?,c#,wpf,dependencies,C#,Wpf,Dependencies,我在WPF中创建了ListBox和ListBoxItem的子类,因为我想向这两个控件添加一些自定义依赖项属性。我用了这个例子: 实现此操作的代码如下所示: protected override DependencyObject GetContainerForItemOverride() { CustomBoxItem container = new CustomBoxItem(); return container; } 这很好

我在WPF中创建了ListBox和ListBoxItem的子类,因为我想向这两个控件添加一些自定义依赖项属性。我用了这个例子:

实现此操作的代码如下所示:

        protected override DependencyObject GetContainerForItemOverride()
    {
        CustomBoxItem container = new CustomBoxItem();

        return container;
    }
这很好,但我的问题是:我必须手动设置所有依赖项属性吗?因为在我的CustomBox.Xaml中,我无法在根标记中设置依赖项属性。我希望在xaml中执行此操作:

<UserControl mycustomdp="SomeValue">

但是当我想要设置绑定时,它将远不如在XAML中设置属性那么优雅。这有可能吗?

听起来好像做错事了。您的自定义ListBoxItem为何如此特殊,以至于无法使用标准ListBoxItem和自定义模板实现相同的功能?我想向ListBoxItem添加依赖项属性什么类型的依赖项属性?为何
container.mycustomdp = "SomeValue";