c# – ItemsControl中的WPF中的可拖动对象?
作者:互联网
我希望能够实现带有可拖动项目的ItemsControl. ItemsControl的原因是我可以在后台绑定到我的ViewModel.
我尝试在画布中使用Thumb控件,它工作得很完美,除非我将它粘在ItemsControl中它就会停止工作.这是我尝试过的:
<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>
背后的代码:
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);
}
最后我的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"));
}
}
}
当我在窗口上执行以下操作时,它可以正常工作:
<Canvas>
<Thumb Canvas.Left="0" Canvas.Top="0" Width="50" Height="50" DragDelta="MyThumb_DragDelta"/>
</Canvas>
但是当我在ItemsControl中使用它时它不再有效.我假设ItemsControl正在注册鼠标事件并覆盖Thumb?
任何人都有一个很好的解决方案来让这个工作?
解决方法:
本我不认为这种方法起初有效,但经过更多的实验,我得到了它.
问题可以归结为:Canvas.Top和Canvas.Left在项目控件中不起作用.但你是正确的,风格是解决问题的方法.这是我提出的解决方案:
<ItemsControl ItemsSource="{Binding Notes}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Thumb Width="150" Height="150" DragDelta="Thumb_DragDelta" />
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding X}" />
<Setter Property="Canvas.Top" Value="{Binding Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
而代码隐藏:
public partial class MainWindow : Window
{
public ObservableCollection<Note> Notes { get; set; }
public MainWindow()
{
InitializeComponent();
DataContext = this;
Notes = new ObservableCollection<Note>();
Notes.Add(new Note(){Title="test", X=100, Y=0});
}
private void Thumb_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
Note n = (Note)((FrameworkElement)sender).DataContext;
n.X += e.HorizontalChange;
n.Y += e.VerticalChange;
}
}
public class Note : INotifyPropertyChanged
{
private string title;
private double x;
private double y;
public double Y
{
get { return y; }
set
{
y = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Y"));
}
}
public double X
{
get { return x; }
set
{
x = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("X"));
}
}
public string Title
{
get { return title; }
set
{
title = value;
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Title"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
标签:c,wpf,itemscontrol,drag 来源: https://codeday.me/bug/20190610/1212963.html