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

C# 在项目网格上方添加文本行

C# 在项目网格上方添加文本行,c#,wpf,grid,panel,C#,Wpf,Grid,Panel,我正在通过WPF构建一个网格,它工作得很好。我需要在网格上方添加一行、一条或一些要显示的内容,这些内容上有几个文本项,这些文本项将由代码填充。我一直在工作,似乎不知道如何在现有(和工作)网格上放置另一个面板。这是我的代码: <Window x:Class="GridWPF.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://

我正在通过WPF构建一个网格,它工作得很好。我需要在网格上方添加一行、一条或一些要显示的内容,这些内容上有几个文本项,这些文本项将由代码填充。我一直在工作,似乎不知道如何在现有(和工作)网格上放置另一个面板。这是我的代码:

<Window x:Class="GridWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Board" SizeToContent="WidthAndHeight" Height="Auto" Width="Auto">
    <Window.Resources>
        <DataTemplate x:Key="DataTemplate_2">
            <Button Content="{Binding}" Height="25" Width="25" Margin="0,0,0,0"/>
        </DataTemplate>
        <DataTemplate x:Key="DataTemplate_1">
            <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_2}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </DataTemplate>
    </Window.Resources>
    <Grid Name="GridBoard" ShowGridLines="True">
        <ItemsControl x:Name="GridItems" ItemTemplate="{DynamicResource DataTemplate_1}"/>
    </Grid>
</Window>


GridItems由锯齿状数组填充,显示良好。我只需要在其上方放置一些文本对象,可以是框,也可以是一个适合网格宽度的水平面板。

我不知道您希望输出是什么样子,但您可以将“GridBoard”
网格
嵌套在另一个
网格
中。新的外部
网格定义了两行,在第一行中,您可以放置您的箱子或任何您喜欢的东西。这可能看起来像这样,例如:

<Window x:Class="GridWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Board" SizeToContent="WidthAndHeight" Height="Auto" Width="Auto">
    <Window.Resources>
        <DataTemplate x:Key="DataTemplate_2">
            <Button Content="{Binding}" Height="25" Width="25" Margin="0,0,0,0"/>
        </DataTemplate>
        <DataTemplate x:Key="DataTemplate_1">
            <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_2}">
                <ItemsControl.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ItemsControl.ItemsPanel>
            </ItemsControl>
        </DataTemplate>
    </Window.Resources>
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
      </Grid.RowDefinitions>
      <Grid Grid.Row="0">
        <TextBlock Text="Put something here ..." />
      </Grid>
      <Grid Grid.Row="1" Name="GridBoard" ShowGridLines="True">
          <ItemsControl x:Name="GridItems" ItemTemplate="{DynamicResource DataTemplate_1}"/>
      </Grid>
    </Grid>
</Window>


请注意,您可以将任何元素用于外部
网格的第一行内容。这取决于你的实际需要。在本例中,我使用了另一个
网格
,其中包含一个
文本块

,最简单的选择可能是添加一个包装网格,并将内部网格放在第二行。因此,您将有第一行(第0行)来放置您需要的任何东西

<Window x:Class="GridWPF.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Board" SizeToContent="WidthAndHeight" Height="Auto" Width="Auto">
<Window.Resources>
    <DataTemplate x:Key="DataTemplate_2">
        <Button Content="{Binding}" Height="25" Width="25" Margin="0,0,0,0"/>
    </DataTemplate>
    <DataTemplate x:Key="DataTemplate_1">
        <ItemsControl ItemsSource="{Binding}" ItemTemplate="{DynamicResource DataTemplate_2}">
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Horizontal"/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>
    </DataTemplate>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid>
        <!-- WHATEVER YOU NEED -->
    </Grid>

    <Grid Name="GridBoard" ShowGridLines="True" Grid.Row="1">
        <ItemsControl x:Name="GridItems" ItemTemplate="{DynamicResource DataTemplate_1}"/>
    </Grid>
</Grid>


实现这一目标有几种方法

1-可以向栅格定义中添加行和列: 半途而废的方法,在你的情况下可能有效,也可能无效。如果不查看
数据模板
,就无法知道

2-顶部为StackPanel,边距应用于Items控件 您可以使用类似的一半解决方案-将
ItemsControl
的上边距设置为固定值,如下所示:

<TextBlock ... Grid.Column="0" />
<TextBlock ... Grid.Column="1" />
<TextBlock ... Grid.Column="2" />
<ItemsControl ... Margin="2,48,2,2" />
我相信你明白了

3-使用GridView-推荐- 不过,这需要您将
ItemsControl
更改为
ListView

是一篇关于WPF的GridView的编写良好的教程。诚然,这可能不是你想要的,但再一次——如果不知道你想要表达什么,就很难找到最好的解决方案

<ItemsControl ... Grid.Row="1" Grid.ColumnSpan="3" />
<ItemsControl ... Margin="2,48,2,2" />
<StackPanel Height="48" VerticalAlignment="Top">
    <TextBlock Text="First Header" Width="300" />
    ...
</StackPanel>