I want to be able to implement an ItemsControl with dragable items. The reason for the ItemsControl is so I can bind to my ViewModel in the background.
I've tried using a Thumb Control in a canvas and it works perfect, except as soon as I stick it in an ItemsControl it stops working. Here is what I tried:
<ItemsControl ItemsSource="{Binding MyItems}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Thumb Canvas.Left="0" Canvas.Top="0" Width="50" Height="50" DragDelta="MyThumb_DragDelta"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
The code behind:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new MainViewModel();
}
private void MyThumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
Canvas.SetLeft((UIElement)sender, Canvas.GetLeft((UIElement)sender) + e.HorizontalChange);
Canvas.SetTop((UIElement)sender, Canvas.GetTop((UIElement)sender) + e.VerticalChange);
}
And finally my ViewModel:
public class MainViewModel : DependencyObject
{
public ObservableCollection<Note> MyItems { get; set;}
public MainViewModel()
{
MyItems = new ObservableCollection<Note>();
MyItems.Add(new Note(){Name="test"});
}
}
public class Note : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private string name;
public string Name
{
get { return name; }
set
{
name = value;
if(PropertyChanged!=null) PropertyChanged(this,new PropertyChangedEventArgs("Name"));
}
}
}
When I do the following on the window it works fine:
<Canvas>
<Thumb Canvas.Left="0" Canvas.Top="0" Width="50" Height="50" DragDelta="MyThumb_DragDelta"/>
</Canvas>
But when I have it in an ItemsControl it no longer works. I assume the ItemsControl is Registering for mouse events and overriding the Thumb?
Anyone have a good solution to get getting this working?