2

In my WPF project I need to have a panel with following behavior. When you click on an item it should go upper top item.

Behavior of an item in the panel

I have several questions regarding this: 1. Should I create my custom Panel and add somehow animation in ArrangeOverride method?

So, I need to adding some event handlers, but without subclassing the element that lay in this panel, and also I need to animate process of going upper. I quite stuck with solution for this problem.

Thanks.

Leonid
  • 1,071
  • 2
  • 15
  • 27

1 Answers1

0

I don't think you need to write a panel or add behavior to your panel, if you model your classes right what you are trying to do is achievable using ItemsControls with ability to select items (i.e. ListBox) - I am assuming you are using MVVM.

The benefit of this approach is that you are not creating and maintaining an additional panel type which you probably don't need. Plus everything in your model and viewmodel can be fully unit tested. I recommend using or extending existing WPF controls and containers whenever possible, however in this case we may get by without it.

ViewModel

public class ItemsVM
{

// You can always keep the items sorted based on you business rules
public ObservableCollection<ItemModel> Items {get;set;}

public ItemVM()
{
Items = new ObservableCollection<ItemModel>(){
new ItemModel(), new ItemModel()
};
}
}

Model

public class ItemModel
{
public bool IsSelected {get;set;}
public ObservableCollection<ItemModel> Items {get;set;}

public ItemModel()
{
Items = new ObservableCollection<ItemModel>();
}
}

View

<ListBox ItemsSource={Binding Items} SelectionMode=Single />

Once the user clicks on one of the items in your ListBox, you should set the IsSelected property of corresponding item in the ItemVM, on this property change, you can add this item to the Items collection of the top element in your list.

You have total control over how you would Template your ItemModel, the top item now has an item in the Items collection, that could be shown the way you have in your diagram.

Regarding Animation This could be done either using code-behind or an attached property, the idea is simple, you know where the user clicked on the ListBox and you should be able to get position of the top element in the ListBox, animate the DataTemplate from current click position to the top of the list

Shrinand
  • 351
  • 3
  • 5
  • This approach won't work for my case. For example you can place in ListView only items derived from ListViewItem as I know. How should it work if I place Grid or some user control into this panel? I know about MVVM but in this case I need some customized panel. I'm developing this panel now. I place sources on GitHub when I finish. – Leonid Feb 27 '13 at 15:49
  • All ItemsControls have an ItemsPanel property that you can set, this ItemPanel could be any WPF Panel. Each individual Item can have an ItemTemplate with your choice of controls. But if you like to create a custom Panel that's your choice. – Shrinand Feb 27 '13 at 19:48
  • I've developed such kind of panel, and If I'll have some free time I will definitely publish sources on the github. – Leonid Sep 27 '13 at 10:28