c# - 具有静态列名称的 GridView 表

标签 c# xaml gridview uwp

所以我想创建一个表,如 GridView 控件,具有静态列名。我可以通过 ItemTemplate 毫无问题地动态添加我的网格列或行。现在的问题是,当我尝试将带有列定义的静态网格添加到我的 GridView 时,我看不到它。你知道为什么吗?

这是我的xaml代码:(没有看到的网格被标记)

<GridView Margin="10" Name="GridViewAllgemein" HorizontalAlignment="Center" VerticalAlignment="Center" ItemsSource="{x:Bind ausstattung}" BorderBrush="Black" IsTapEnabled="True" IsItemClickEnabled="True">
        <Grid Background="Red" Height="50"> //cannot see this grid
            <Grid.ColumnDefinitions> //cannot see this grid
                <ColumnDefinition Width="200"/> //cannot see this grid
                <ColumnDefinition Width="200"/> //cannot see this grid
                <ColumnDefinition Width="200"/> //cannot see this grid
                <ColumnDefinition Width="200"/> //cannot see this grid
            </Grid.ColumnDefinitions> //cannot see this grid
            <TextBlock Grid.Column="0" Text="Test"></TextBlock> //cannot see this grid
        </Grid> //cannot see this grid
        <GridView.ItemTemplate>
            <DataTemplate x:DataType="data:Ausstattung">
                <Grid Background="{ThemeResource SystemControlBackgroundAccentBrush}" Height="40" Margin="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition Width="200"/>
                        <ColumnDefinition Width="200"/>
                    </Grid.ColumnDefinitions>


                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="0" Foreground="White" FontSize="14" Text="{x:Bind Beschreibung}" TextWrapping="Wrap"/>
                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"  Grid.Column="1" Foreground="White" FontSize="14" Text="{x:Bind Ausgabedatum}" TextWrapping="Wrap"/>
                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"  Grid.Column="2" Foreground="White" FontSize="14" Text="{x:Bind Rückgabedatum}" TextWrapping="Wrap"/>
                    <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center"  Grid.Column="3" Foreground="White" FontSize="14" Text="{x:Bind Anzahl}" TextWrapping="Wrap"/>
                </Grid>
            </DataTemplate>
        </GridView.ItemTemplate>
    </GridView>

这是结果 enter image description here

最佳答案

The problem now is, when I try to add a static grid with columndefinitions to my GridView i cannot see it. Do you know why?

这是因为 GridView 是一个 ItemsControl,它不能直接包含子元素。

It must have items in its Items collection before it can show anything on the screen. To populate the view, you can add items directly to the Items collection, or set the ItemsSource property to a data source. See here

正如@Ipsit Gaur 在评论中指出的那样,在这种情况下,一种选择是将带有页眉的网格放在 GridView 之前,因为 GridView 本身没有创建页眉的概念。

顺便说一句。 telerik 开源了它的 UWP 控件,在某些情况下你可以免费使用它。他们有一个 Grid control with a built in functionality for this .

Syncfusion 也有与 a similar control called SfDataGrid 类似的社区许可证

*我不是要为产品做广告,我只是列出了您可以用来解决问题的免费控件。

关于c# - 具有静态列名称的 GridView 表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44695462/

相关文章:

c# - Visual Studio正在寻找没有publicKeyToken的旧Nuget包

wpf - 找不到与引用绑定(bind)的源... databound ListView 问题

asp.net - 如何为 ASP.NET GridView 列设置皮肤?

c# - 如何设置按钮点击事件的优先级?

c# - WebClient 抛出 SocketException(无效参数)

C#:如何对十六进制数字进行位移

.net - 页面加载时 Gridview 不显示

wpf - 进度条条样式右半径

c# - 如何在 WPF 中正确绑定(bind)我的模型?

c# - 数据格式问题。将 GridView 导出到 Excel