Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataGrid: DataGridTextColumn with StringFormat binding corrupts the ItemsSource #102

Open
sards3 opened this issue Aug 1, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@sards3
Copy link

sards3 commented Aug 1, 2024

Describe the bug

If the DataGrid's ItemsSource is a collection of objects containing an int property (for example), and the grid has a DataGridTextColumn with the Binding field binding to that int property using a StringFormat, the DataGrid will corrupt the ItemsSource collection as you scroll through the DataGrid.

To Reproduce

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:AvaloniaDatagridBug.ViewModels"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="200" d:DesignHeight="450" Width="200" Height="450"
        x:Class="AvaloniaDatagridBug.Views.MainWindow"
        x:DataType="vm:MainWindowViewModel"
        Icon="/Assets/avalonia-logo.ico"
        Title="AvaloniaDatagridBug">
    <Design.DataContext>
        <vm:MainWindowViewModel/>
    </Design.DataContext>
	
    <DataGrid ItemsSource="{Binding IntArray}" Height="400">
        <DataGrid.Columns>
            <DataGridTextColumn 
                x:DataType="vm:IntWrapper"
                Header="Int Value" 
                IsReadOnly="True" 
                Binding="{Binding IntValue, StringFormat={}{0:X3}}"/>
        </DataGrid.Columns>
    </DataGrid>
</Window>
using System.Linq;
namespace AvaloniaDatagridBug.ViewModels;

public record IntWrapper(int IntValue);
public partial class MainWindowViewModel : ViewModelBase
{
    public IntWrapper[] IntArray { get; } = Enumerable.Range(0, 50).Select(i => new IntWrapper(i)).ToArray(); 
}

In this example, as you scroll through the grid, the numbers should be in hexadecimal sequence. But once you get past 00F, the numbers become corrupted.

Expected behavior

DataGrid should not corrupt the ItemsSource as you scroll through the grid.

Avalonia version

Latest nightly

OS

Windows

Additional context

No response

@sards3 sards3 added the bug Something isn't working label Aug 1, 2024
@MrJul MrJul transferred this issue from AvaloniaUI/Avalonia Mar 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant