Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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_Itemscontrol - Fatal编程技术网

C# 如何动态显示与父项控件类似的项控件?

C# 如何动态显示与父项控件类似的项控件?,c#,wpf,itemscontrol,C#,Wpf,Itemscontrol,数据库中有两个表:节表和考试表。节(在节表中)有一个或多个考试(在考试表中) 对于节中的每个节,我想在ItemsControl中显示节名以及该节的名称下的考试。这幅图也许能解释更多 因此,我从数据库中获取节的列表,并将其用作ItemsControl的自定义模板的ItemsSource;ItemsControl将显示每个部分下的部分,ItemsControl将显示此部分的考试 下面是sections Items控件的模板 <Style TargetType="ItemsC

数据库中有两个表:节表和考试表。节(在节表中)有一个或多个考试(在考试表中)

对于节中的每个节,我想在ItemsControl中显示节名以及该节的名称下的考试。这幅图也许能解释更多

因此,我从数据库中获取节的列表,并将其用作ItemsControl的自定义模板的ItemsSource;ItemsControl将显示每个部分下的部分,ItemsControl将显示此部分的考试

下面是sections Items控件的模板

    <Style TargetType="ItemsControl" x:Key="ListSectionsItemsControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ItemsPresenter/>
                </ControlTemplate>
            </Setter.Value>

        </Setter>

        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="{Binding}" VerticalContentAlignment="Center" FontSize="14" FontWeight="Bold"/>
                        <ItemsControl Name="AnalysesItemsTest" Style="{DynamicResource ListAnalysesItemsControl}"/>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>

        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="3" Background="White"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>



    </Style>
  <Style TargetType="ItemsControl" x:Key="ListAnalysesItemsControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ItemsPresenter/>
                </ControlTemplate>
            </Setter.Value>

        </Setter>

        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox VerticalContentAlignment="Center"/>
                        <Label Content="{Binding}" VerticalContentAlignment="Center" FontSize="16" FontWeight="Bold"/>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>

        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="2" Background="White"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>



    </Style>

对于考试,我还创建了ItemsControl模板

    <Style TargetType="ItemsControl" x:Key="ListSectionsItemsControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ItemsPresenter/>
                </ControlTemplate>
            </Setter.Value>

        </Setter>

        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <Label Content="{Binding}" VerticalContentAlignment="Center" FontSize="14" FontWeight="Bold"/>
                        <ItemsControl Name="AnalysesItemsTest" Style="{DynamicResource ListAnalysesItemsControl}"/>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>

        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="3" Background="White"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>



    </Style>
  <Style TargetType="ItemsControl" x:Key="ListAnalysesItemsControl">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate>
                    <ItemsPresenter/>
                </ControlTemplate>
            </Setter.Value>

        </Setter>

        <Setter Property="ItemTemplate">
            <Setter.Value>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <CheckBox VerticalContentAlignment="Center"/>
                        <Label Content="{Binding}" VerticalContentAlignment="Center" FontSize="16" FontWeight="Bold"/>
                    </StackPanel>
                </DataTemplate>
            </Setter.Value>
        </Setter>

        <Setter Property="ItemsPanel">
            <Setter.Value>
                <ItemsPanelTemplate>
                    <UniformGrid Columns="2" Background="White"/>
                </ItemsPanelTemplate>
            </Setter.Value>
        </Setter>



    </Style>

在我的Xaml中,我创建了一个ItemsControl,它使用sections ItemsControl的模板样式,如下所示:

<ItemsControl Name="SectionsItemsControl" Style="{DynamicResource ListSectionsItemsControl}"/>

我成功地获得了分区名称

但我无法显示该部分的考试

我尝试为tests ItemsControl(在sections ItemsControl的样式模板中)指定一个名称,以便可以在代码隐藏中为其定义itemsSource,但可以在代码隐藏中获取它。zI还试图定义ItemsSource,但也没有定义任何内容

请问你怎么做?
感谢您的帮助。

似乎没有任何内容。设置了两个itemscontrol的itemssource。内部itemscontrol如何知道它应该知道从何处获取考试?我在后面的代码中定义了ItemsSource of Sections itemscontrol。这就是我试图对考试项控件所做的。