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

Checkbox in DataGridTemplateColumn : Clicking on checkbox generates ghost checkbox #4959

Open
1 of 14 tasks
FPotel opened this issue Dec 11, 2023 · 6 comments
Open
1 of 14 tasks
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior DataGrid 🔠 Issues on DataGrid control needs attention 👋 needs triage 🔍

Comments

@FPotel
Copy link

FPotel commented Dec 11, 2023

Describe the bug

I created a UserControl that contains a DataGrid, which contains one DataGridTemplateColumn, which contains a checkbox.
The IsChecked of that checkbox is binded with an ObservableProperty of a POCO class.
When clicking randomly on the checkboxes, and setting a breakpoint into the event OnChecked, we can see that we pass several times into it, and the datacontext linked to the checkbox is sometimes null. I set another breakpoint in the code generated by my UserControl (.g.cs file) into the Connect function, and I can observe that the thread pass several times into the code of the checkbox creation.
The fact the checkbox is binded to an Observable Property makes the bug happen (with a regular property, no bug). If I delete the ObservableProperty tag on the property, and I raise a OnPropertyChanged event in the setter, I also observe the bug.

Regression

No response

Reproducible in sample app?

  • This bug can be reproduced in the sample app.

Steps to reproduce

Here is the code for :
The View (xaml + xaml.cs)
-CustomDatagrid
-MainWindow

The model:(the POCO class to view)
-CustomDataNode

/////////////////////////////////////////////////////
/////////////////////// CustomDatagrid XAML ////////////////////////
/////////////////////////////////////////////////////

<UserControl
    x:Class="BugDatagrid.Views.CustomDatagrid"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    xmlns:controls="using:CommunityToolkit.WinUI.UI.Controls"
    x:Name="View">

    <Grid>
        <controls:DataGrid ItemsSource="{Binding ElementName=View,Path=ListNodes, Mode=OneWay}"  
                           AutoGenerateColumns="False"
                           HorizontalAlignment="Stretch"
                           HorizontalContentAlignment="Stretch"
                           x:Name="DataGrid">

            <controls:DataGrid.Columns>

                <controls:DataGridTemplateColumn Header="Display" >
                    <controls:DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Grid>
                                <CheckBox IsChecked="{Binding Path=IsDisplayed, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
                                            Checked="ToggleButton_OnChecked"
                                          Unchecked="ToggleButton_OnUnchecked"/>
                            </Grid>
                        </DataTemplate>
                    </controls:DataGridTemplateColumn.CellTemplate>
                </controls:DataGridTemplateColumn>
            </controls:DataGrid.Columns>
        </controls:DataGrid>
    </Grid>
</UserControl>

////////////////////////////////////////////////////////
/////////////////////// CustomDatagrid XAML.CS ////////////////////////
////////////////////////////////////////////////////////
  public sealed partial class CustomDatagrid : UserControl
  {
      public CustomDatagrid()
      {
          this.InitializeComponent();
      }

      public static readonly DependencyProperty ListNodesProperty = DependencyProperty.Register(
          nameof(ListNodes), typeof(List<CustomDataNode>), typeof(CustomDatagrid), new PropertyMetadata(default(List<CustomDataNode>)));

      public List<CustomDataNode> ListNodes
      {
          get { return (List<CustomDataNode>)GetValue(ListNodesProperty); }
          set
          {
              SetValue(ListNodesProperty, value);
          }
      }

      private void ToggleButton_OnChecked(object sender, RoutedEventArgs e)
      {
          if ((sender as CheckBox)?.DataContext == null)
          {

          }
      }
}
///////////////////////////////////////////////////////////
/////////////////////// CustomDataNode (POCO CLASS )////////////////////////
///////////////////////////////////////////////////////////
public partial class CustomDataNode :ObservableObject
{
    public CustomDataNode()
    {
    }

    [ObservableProperty] private bool _isDisplayed;
}

/////////////////////////////////////////////////////
/////////////////////// MainWindow XAML ////////////////////////
/////////////////////////////////////////////////////
<Window
    x:Class="BugDatagrid.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:BugDatagrid"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:views="using:BugDatagrid.Views"
    mc:Ignorable="d"
    x:Name="View">

    <views:CustomDatagrid ListNodes="{Binding ElementName=View,Path=MyDemoList,Mode=OneWay}"/>
</Window>

/////////////////////////////////////////////////////
/////////////////////// MainWindow XAML.cs ////////////////////////
/////////////////////////////////////////////////////
    /// <summary>
    /// An empty window that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainWindow : Window
    {
        public MainWindow()
        {
            this.InitializeComponent();
        }
        public List<CustomDataNode> MyDemoList { get; set; } = new() { new CustomDataNode(), new CustomDataNode(), new CustomDataNode() };
    }

Expected behavior

I can't explain why the fact I binded the IsChecked property of my Checkbox to an ObservableProperty in my model triggers the creation of ghost checkbox (with null datacontext).
I found a workaround for my code, but thouht it was worth it reporting this behavior.

Screenshots

No response

Windows Build Number

  • Windows 10 1809 (Build 17763)
  • Windows 10 1903 (Build 18362)
  • Windows 10 1909 (Build 18363)
  • Windows 10 2004 (Build 19041)
  • Windows 10 20H2 (Build 19042)
  • Windows 10 21H1 (Build 19043)
  • Windows 11 21H2 (Build 22000)
  • Other (specify)

Other Windows Build number

Windows 11 22H2

App minimum and target SDK version

  • Windows 10, version 1809 (Build 17763)
  • Windows 10, version 1903 (Build 18362)
  • Windows 10, version 1909 (Build 18363)
  • Windows 10, version 2004 (Build 19041)
  • Other (specify)

Other SDK version

No response

Visual Studio Version

2022

Visual Studio Build Number

No response

Device form factor

No response

Nuget packages

CommunityToolkit.WinUI.UI.Controls 7.1.2
CommunityToolkit.Mvvm 8.2.2

Additional context

No response

Help us help you

No.

@FPotel FPotel added the bug 🐛 An unexpected issue that highlights incorrect behavior label Dec 11, 2023
@ghost ghost added the needs triage 🔍 label Dec 11, 2023
@ghost
Copy link

ghost commented Dec 11, 2023

Hello FPotel, thank you for opening an issue with us!

I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌

@ghost ghost added the needs attention 👋 label Dec 26, 2023
@ghost
Copy link

ghost commented Dec 26, 2023

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

1 similar comment
@ghost
Copy link

ghost commented Jan 10, 2024

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

@Arlodotexe Arlodotexe added the DataGrid 🔠 Issues on DataGrid control label Jan 23, 2024
@ghost
Copy link

ghost commented Feb 7, 2024

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

2 similar comments
@ghost
Copy link

ghost commented Feb 22, 2024

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

@ghost
Copy link

ghost commented Mar 9, 2024

This issue has been marked as "needs attention 👋" due to no activity for 15 days. Please triage the issue so the fix can be established.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior DataGrid 🔠 Issues on DataGrid control needs attention 👋 needs triage 🔍
Projects
None yet
Development

No branches or pull requests

2 participants