Search code examples
c#xamlvisibility

Issue WPF Datagrid maybe bug?


I am generating a datagrid dynamically and the only problem I have is that it creates an extra row at the end, it is something visual because when I delete by code the last row continues to appear.

<Grid>
    <DataGrid x:Name="dataGridPatrullas" ItemsSource="{Binding Patrullas}" AutoGenerateColumns="False" CanUserSortColumns="False" SelectionMode="Single" HorizontalAlignment="Center" VerticalAlignment="Center" Width="328">

                    <!-- Estilo para las celdas de datos -->
                    <DataGrid.CellStyle>
                        <Style TargetType="DataGridCell">
                            <Setter Property="Background" Value="#E0E0E0"/>
                            <!-- Fondo de las celdas -->
                            <Setter Property="Foreground" Value="Black"/>
                            <!-- Color del texto en las celdas -->
                            <Setter Property="HorizontalAlignment" Value="Center"/>
                        </Style>
                    </DataGrid.CellStyle>

                    <!-- Estilo para las filas -->
                    <DataGrid.RowStyle>
                        <Style TargetType="DataGridRow">
                            <Setter Property="Background" Value="White"/>
                            <!-- Fondo de las filas -->
                            <Setter Property="Foreground" Value="Black"/>
                            <!-- Color del texto en las filas -->
                        </Style>
                    </DataGrid.RowStyle>

                    <!-- Estilo para el encabezado -->
                    <DataGrid.ColumnHeaderStyle>
                        <Style TargetType="DataGridColumnHeader">
                            <Setter Property="Background" Value="#333333"/>
                            <!-- Fondo del encabezado -->
                            <Setter Property="Foreground" Value="White"/>
                            <!-- Color del texto en el encabezado -->
                            <Setter Property="BorderBrush" Value="Black"/>
                            <!-- Color del borde del encabezado -->
                            <Setter Property="BorderThickness" Value="0,0,1,1"/>
                            <!-- Grosor del borde del encabezado -->
                        </Style>
                    </DataGrid.ColumnHeaderStyle>

                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Nombre" Binding="{Binding Nombre}">
                            <DataGridTextColumn.ElementStyle>
                                <Style>
                                    <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTemplateColumn Header=" Recalcular Ruta ">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="Recalcular" Click="RecalcularButton_Click"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTextColumn Header="Tiempo" Binding="{Binding Tiempo}">
                            <DataGridTextColumn.ElementStyle>
                                <Style>
                                    <Setter Property="TextBlock.TextAlignment" Value="Center"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridCheckBoxColumn Header=" Deshabilitar " Binding="{Binding Deshabilitar}">
                            <DataGridCheckBoxColumn.ElementStyle>
                                <Style>
                                    <EventSetter Event="CheckBox.Checked" Handler="PatrullaCheckBox_Checked" />
                                    <EventSetter Event="CheckBox.Unchecked" Handler="PatrullaCheckBox_Unchecked"/>
                                </Style>
                            </DataGridCheckBoxColumn.ElementStyle>
                        </DataGridCheckBoxColumn>
                        <DataGridCheckBoxColumn Header=" En Ruta " Binding="{Binding EnRuta}">
                            <DataGridCheckBoxColumn.ElementStyle>
                                <Style>
                                    <EventSetter Event="CheckBox.Checked" Handler="RutaCheckBox_Checked"/>
                                    <EventSetter Event="CheckBox.Unchecked" Handler="RutaCheckBox_Unchecked"/>
                                </Style>
                            </DataGridCheckBoxColumn.ElementStyle>
                        </DataGridCheckBoxColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>

Code:

using System;

namespace WpfPatrols { using System.Collections.ObjectModel;

public class MainWindowViewModel
{
    public ObservableCollection<Patrulla> Patrullas { get; set; }

    public MainWindowViewModel()
    {
        Patrullas = GeneratePatrullas(10);
    }

    private ObservableCollection<Patrulla> GeneratePatrullas(int count)
    {
        ObservableCollection<Patrulla> patrullas = new ObservableCollection<Patrulla>();

        for (int i = 1; i <= count; i++)
        {
            Patrulla patrulla = new Patrulla
            {
                Nombre = "Patrulla" + i,
                Tiempo = "00:00",
                Habilitado = false,
                EnRuta = false
            };

            patrullas.Add(patrulla);
        }

        return patrullas;
    }

}

}

Like i say everything works fine the only thing is that last visual row...

Best regards


Solution

  • check DataGrid Properties like: AutoGenerateColumns="False" CanUserAddRows="False"