KhaosCoders / wpf-kanban

A Kanban Board control for WPF
MIT License
19 stars 10 forks source link

Removed card is still visible / Column property CardCount is zero after refresh #11

Closed KayOpper closed 1 year ago

KayOpper commented 1 year ago

Hello.

I have several columns and swimlanes in my Kanbanboard, of course. If I delete a card via the context menu, it will be deleted from the observable collection successfully but it is still visible in the Kanbanboard. If I move then with the mouse over this card, I get a NullReferenceException from the CanDragCardEvent.

So I try to help myself with Items.Refresh(). This updates the view now but I get the next issue as I need the CardCount values of the columns. The correct CardCount is shown in the header of the coulmns in the Kanbanboard but the source code returns zero for all columns. This happens only after Items.Refresh().

So what I need, is to delete a card which should disappear from the Kanbanboard. After that, the source code should deliver the correct CardCount values of the columns. I hope you have some ideas how to fix this.

Regards

Khaos66 commented 1 year ago

Hi,

looks like you're right. I've added your use case to the example app and get the same result. I'll have a look at it ;)

Khaos66 commented 1 year ago

I think I fixed the issue. Will release a new version of the Nuget soon

KayOpper commented 1 year ago

Hello.

Thanks for dealing with my issue very fast. Now I am still waiting for the update version on NuGet. It should also always contain the correct property value for the CardCount, especially after Items.Refresh()

Khaos66 commented 1 year ago

Thanks for reminding me! Sorry I lost track of this... I'll release it now ;)

Khaos66 commented 1 year ago

Here you go: https://www.nuget.org/packages/WPF-Kanban/2.0.2

Khaos66 commented 1 year ago

Please let me know it this fixes the issue for you ;)

KayOpper commented 1 year ago

Yes, you solved it. Thank you.

KayOpper commented 1 year ago

Sorry, it is only fixed half ;-). After Items.Refresh(), the CardCount property value of each column returns zero even if they contain cards. The correct CardCount is shown in the header of the coulmns in the Kanbanboard but the property getter returns zero. Items.Refresh() is no longer necessary for me at the moment but it is still not correct.

Khaos66 commented 1 year ago

Hey sorry to hear that! I was trying to reproduce your issue in the sample app, but wasn't able. This is what I was trying: https://github.com/KhaosCoders/wpf-kanban/commit/ba8d2a8cebc537e2318e1c24fa4a98f57fbc8ea1 But all the columns show the correct numbers after I hit the Refresh button. So you must be doing something else.

Can you please hand me an example with your issue?

KayOpper commented 1 year ago

Yes the columns show the correct value but the return value of the property getter is zero. This happens only direct after Items.Refresh(). I do something like this:

    ordersKanbanBoard.Items.Refresh();
    foreach (KanbanColumn column in ordersKanbanBoard.Columns)
    {
        if (column.CardCount == 0)
            column.IsCollapsed = true;
        else
            column.IsCollapsed = false;

        Console.WriteLine("Column {0} has {1} cards", column.ColumnValue, column.CardCount);   // is always zero
    }
Khaos66 commented 1 year ago

Yes the columns show the correct value but the return value of the property getter is zero. This happens only direct after Items.Refresh(). I do something like this:

    ordersKanbanBoard.Items.Refresh();
    foreach (KanbanColumn column in ordersKanbanBoard.Columns)
    {
        if (column.CardCount == 0)
            column.IsCollapsed = true;
        else
            column.IsCollapsed = false;

        Console.WriteLine("Column {0} has {1} cards", column.ColumnValue, column.CardCount);   // is always zero
    }

That's aktually smart to auto hide empty columns... I'll have a look. Maybe there should be a AutoHide Property on the column, so you don't have to do this ;)

Khaos66 commented 1 year ago

@KayOpper I guess what you're doing is not how the .NET Framework is supposed to work.

The call to Items.Refresh() causes a Reset of the cards collection (that's why all CardCount properties are 0). It also causes the layout to re-create the cards on the next render cycle. Since there is no pause between your call to Refresh and the foreach loop, where the Dispatcher could run a render cycle, the cards are not yet re-created and therefore the CardCount is not yet updated. The UI is showing the correct counts, as the following render cycle updates the UI and creates the cards correctly.

Solution: Dispatcher with priority Loaded (one priority below Render)

kanBoard.Items.Refresh();

Dispatcher.BeginInvoke(new Action(() =>
{
    foreach (KanbanColumn column in kanBoard.Columns)
    {
        column.IsCollapsed = column.CardCount == 0;
        Console.WriteLine("Column {0} has {1} cards", column.ColumnValue, column.CardCount);   // has correct value
    }
}), System.Windows.Threading.DispatcherPriority.Loaded);
KayOpper commented 1 year ago

Ah okay. I understand. Then it is okay for me. Mainly, I was confused that the CardCount values from the UI and the property getter were not the same :-)

Khaos66 commented 1 year ago

I've added a new AutoCollapse property to the KanbanColumn. I'll release a new version later today

Please close this issue if your issue has been resolved ;)