KhaosCoders / wpf-kanban

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

Drag and Drop - get the object behind the KanbanCard #4

Closed KayOpper closed 3 years ago

KayOpper commented 3 years ago

Hi. I am very happy that I found this Kanban Board. My question is how can I get the object represented by the KanbanPlainCard when I handle the CanDrag and CanDrop events? The Itemssource is connected with an ObservableCollection of database objects. The DataContext is a ViewModel which fills the ObservableCollection with the database objects.

Now some cards should not be dragged. For that I have to verify the set values in the object behind the card. Also I have to restrict where to drop the cards. I found the TargetSwimlane and TargetColumn values but no properties for the origin values. Also I must update the database object after dropping the card. So I guess it is not possible to use SelectedItem to get my object assigned to the card dropped. All I found is the Id property of the KanbanCard which I can use. But this is a little bit inconvenient to use this for finding my object. Is there not a better solution? Thanks

Khaos66 commented 3 years ago

Hi! And I'm happy that it helps somebody ;)

In each of the events (CanDragCard, CanDropCard and CardMoved) you get the KanbanCard instance via the e parameter. You can find it in the sample included in this repo. Your ViewModel instance is the DataContext of the card. So your event handler could look like this:

private void kanBoard_CanDragCard(object sender, KC.WPF_Kanban.CanDragCardEventArgs e)
{
    if (e.Card.DataContext is CardData data)
    {
        e.CanDrag = data.Duration > 5;
    }
}

The same goes for the CanDrop:

private void kanBoard_CanDropCard(object sender, KC.WPF_Kanban.CanDropCardEventArgs e)
{
    if (e.Card.DataContext is CardData data)
    {
        e.CanDrop = e.TargetColumn.ColumnValue.ToString()[0] == 'D' && data.Duration > 5;
    }
}

And for CardMoved it should be the same, too ;) There you can update the datebase.

KayOpper commented 3 years ago

Perfect. Thank you for the very quick response.