【问题标题】:DataGrid table are shown additional unnecessary empty rowDataGrid 表显示额外的不必要的空行
【发布时间】:2014-02-09 15:23:59
【问题描述】:

我使用以下代码,例如,当集合是包含数据的字段时,我在屏幕上看到 2 行包含数据,列表末尾还有一个空行。

如何省略最后一个空行?该系列只有 2 个条目。集合项只有 2 个条目。

<DataGrid RowHeaderWidth="0" ItemsSource="{Binding Items}" AutoGenerateColumns="False">
    <!--<DataGrid.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue" />
        </DataGrid.Resources>-->
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="1" Width="*">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox IsChecked="{Binding Column1, UpdateSourceTrigger=PropertyChanged}"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>

        <DataGridTextColumn Binding="{Binding Column2}" Header="2" Width="*" />
        <DataGridTextColumn Binding="{Binding Column3}" Header="3" Width="*" />
        <DataGridTextColumn Binding="{Binding Column4}" Header="4" Width="*" />
        <DataGridTextColumn Binding="{Binding Column5}" Header="5" Width="*" />
    </DataGrid.Columns> 
</DataGrid>

【问题讨论】:

    标签: wpf xaml mvvm datagrid


    【解决方案1】:

    尝试CanUserAddRows设置false,注册默认为true:

    <DataGrid Name="MyDataGrid" 
              CanUserAddRows="False" ... />
    

    引用MSDN:

    当此属性设置为 true 时,DataGrid 底部会显示一个空白行。用户可以在空白行中输入新项目。添加新行会将项目添加到 ItemsSource。

    【讨论】:

    • 即使“CanUserAddRows”在设计器的“属性”窗口中为 false,如果在 XAML 中未明确设置为 False (CanUserAddRows="False"),这将不起作用。
    猜你喜欢
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-30
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多