Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.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# 集合上的数据模板_C#_Wpf_Xaml_Custom Controls - Fatal编程技术网

C# 集合上的数据模板

C# 集合上的数据模板,c#,wpf,xaml,custom-controls,C#,Wpf,Xaml,Custom Controls,我在这里有一个名为DockGroup的自定义控件类。我想将DockGroup和databind的样式模板应用于Items依赖项属性(使用自定义datatemplate) [ContentProperty(@“Items”)] 公共类DockGroup:控件 { 公共静态只读从属属性ItemsProperty= DependencyProperty.Register(“项目”、typeof(ObservableCollection)、typeof(DockGroup)、new PropertyMe

我在这里有一个名为DockGroup的自定义控件类。我想将DockGroup和databind的样式模板应用于Items依赖项属性(使用自定义datatemplate)

[ContentProperty(@“Items”)]
公共类DockGroup:控件
{
公共静态只读从属属性ItemsProperty=
DependencyProperty.Register(“项目”、typeof(ObservableCollection)、typeof(DockGroup)、new PropertyMetadata(默认值(ObservableCollection));
公共可观测收集项目
{
get{return(ObservableCollection)GetValue(ItemsProperty);}
set{SetValue(ItemsProperty,value);}
}
公共文档组()
{
Items=新的ObservableCollection();
DataContext=项目;
}
}
这是DockGroup的样式,也包含DataTemplate

<Style TargetType="{x:Type local:DockGroup}">
    <Style.Resources>
        <DataTemplate x:Key="Items" DataType="{x:Type UIElement}">
            <Label>I DON'T KNOW WHAT TO PUT IN DATATEMPLATE</Label>
        </DataTemplate>
    </Style.Resources>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:DockGroup}">
                <ContentPresenter ContentSource="{Binding}"/>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

我不知道在数据模板中放什么
下面是我想要测试的XAML示例

<local:DockGroup>
     <Label Background="#FFF0F077">Hello World!</Label>
     <Label Background="#FFF0F077">Hello World 2!</Label>
</local:DockGroup>

你好,世界!
你好,世界2!

当我运行项目时,我得到一个空白窗口。而且,我似乎无法找出问题所在。

您可以使用ItemsControl设置控件的样式以使其正常工作:

<Style TargetType="{x:Type local:DockGroup}">
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate>
      <ItemsControl ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}}"/>
    </ControlTemplate>
  </Setter.Value>
</Setter>


您可以使用ItemsControl设置控件样式以使其正常工作:

<Style TargetType="{x:Type local:DockGroup}">
<Setter Property="Template">
  <Setter.Value>
    <ControlTemplate>
      <ItemsControl ItemsSource="{Binding Items, RelativeSource={RelativeSource TemplatedParent}}"/>
    </ControlTemplate>
  </Setter.Value>
</Setter>


首先,标签如何知道它们是DockGroup的内容可能是:
DockGroup
ContentPropertyAttribute
指定应将
下的任何元素添加到
Items
属性中(这是一个集合,意味着它将允许
DockGroup
下的多个元素)。假定DataTemplate用于逻辑对象,而不是UIElement,例如类“Person”可以有一个描述其外观的DataTemplate..您似乎尝试将DataTemplates应用于Labelsiv'e尝试了一些方法,但似乎没有任何效果…首先,您需要添加一个静态构造函数,以删除所有默认元数据Static DockGroup(){DefaultStyleKeyProperty.OverrideMetadata(typeof(DockGroup),新的FrameworkPropertyMetadata(typeof(DockGroup));}我还尝试从items控件派生并给它一个itemtemplate…只是有点奇怪,你用另一个UIElement替换了一个UIElement…是的,我不确定什么都没用。首先,标签如何知道它们是DockGroup的内容可能是:
contentPropertyAttributete
for
DockGroup
指定应将
下的任何元素添加到
Items
属性(这是一个集合,意味着它将允许
DockGroup
下的多个元素)。假定DataTemplate用于逻辑对象,而不是UIElement,例如类“Person”可以有一个描述其外观的DataTemplate..您似乎尝试将DataTemplates应用于Labelsiv'e尝试了一些方法,但似乎没有任何效果…首先,您需要添加一个静态构造函数,以删除所有默认元数据Static DockGroup(){DefaultStyleKeyProperty.OverrideMetadata(typeof(DockGroup),新的FrameworkPropertyMetadata(typeof(DockGroup));}iv'e也尝试从items控件派生并给它一个itemtemplate…只是有点奇怪,你用一个其他UIElement替换了一个UIElement…是的,我不确定似乎什么都没用。