我ICollectionView看起来像
public ICollectionView UsersCollectionView
{
get
{
var view = CollectionViewSource.GetDefaultView(this);
view.GroupDescriptions.Add(new PropertyGroupDescription("SeriesName"));
view.SortDescriptions.Add(new SortDescription("CreationDate", ListSortDirection.Ascending));
view.SortDescriptions.Add(new SortDescription("DocumentTypeId", ListSortDirection.Ascending));
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
我想使用拖放来更改项目系列名称和列表视图中的位置,例如,如何做到这一点
--- ScienceFiction
------------> Book1
------------> Book2
--- History
------------> Book3
------------> Book4
Run Code Online (Sandbox Code Playgroud)
如果在ScienceFiction中Iragedd and droped book3,那么输出应该是
--- ScienceFiction
------------> Book1
------------> Book2
------------> Book3
--- History
------------> Book4
Run Code Online (Sandbox Code Playgroud)
我像这样使用xaml代码:
<UserControl.Resources>
<Style x:Key="ContainerStyle" TargetType="{x:Type GroupItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate>
<Expander Header="{Binding Name}" IsExpanded="True">
<ItemsPresenter />
</Expander>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Grid>
<ListBox x:Name="lbPersonList" Margin="19,17,162,25" AlternationCount="2" ItemsSource="{Binding}">
<ListBox.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource ContainerStyle}"/>
</ListBox.GroupStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
Run Code Online (Sandbox Code Playgroud)
抗苗剂,
首先修改ListviewItem的Style。它是包含列表框的每一行(数据模板实例)的容器。这是在行级别管理拖放的好地方(不是行的控件,DataTemplate 中可能有很多控件)。在 Visual Studio 中,选择列表框,右键单击,编辑其他模板/编辑生成的项目容器(ItemContainerStyle)/编辑副本
在创建的 ListBoxItemStyle 中,在 setter 中添加这三个声明:
<EventSetter Event="ListBoxItem.DragOver" Handler="ListBoxItemDragOver"/>
<EventSetter Event="ListBoxItem.Drop" Handler="ListBoxItemDrop"/>
<EventSetter Event="ListBoxItem.PreviewMouseMove" Handler="ListBoxItemPreviewMouseMove"/>
Run Code Online (Sandbox Code Playgroud)
在 ListBox 上将 AllowDrop 属性设置为 true :
<ListBox x:Name="listboxBooks" AllowDrop="True">
Run Code Online (Sandbox Code Playgroud)
然后在 .xaml.cs 代码中实现处理程序:
#region DnD management
private Book sourceBook;
private void ListBoxItemPreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton != MouseButtonState.Pressed)
return;
var listboxItem = sender as ListBoxItem;
if (listboxItem == null)
return;
sourceBook = listboxItem.DataContext as Book;
if (sourceBook == null)
return;
var data = new DataObject();
data.SetData(sourceBook);
// provide some data for DnD in other applications (Word, ...)
data.SetData(DataFormats.StringFormat, sourceBook.ToString());
DragDropEffects effect = DragDrop.DoDragDrop(listboxItem, data, DragDropEffects.Move | DragDropEffects.Copy);
}
private void ListBoxItemDrop(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(typeof(Book)))
return;
var listBoxItem = sender as ListBoxItem;
if (listBoxItem == null)
return;
var targetBook = listBoxItem.DataContext as Book;
if (targetBook != null)
{
viewModel.RecategorizeBook(sourceBook, targetBook.Category);
}
e.Handled = true;
}
private void ListBoxItemDragOver(object sender, DragEventArgs e)
{
Debug.WriteLine(e.Effects);
if (!e.Data.GetDataPresent(typeof(Book)))
{
e.Effects = DragDropEffects.None;
e.Handled = true;
}
}
private void GroupItemDrop(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent(typeof(Book)))
return;
var groupItem = sender as GroupItem;
if (groupItem == null)
return;
dynamic targetGroup = groupItem.DataContext;
if (targetGroup != null)
{
// here I change the category of the book
// and refresh the view of the collectionViewSource ( see link to project zipped further)
viewModel.RecategorizeBook(sourceBook, targetGroup.Name as String);
}
e.Handled = true;
}
#endregion
Run Code Online (Sandbox Code Playgroud)
请注意,我还在处理程序中的组标头上实现了 Drop 管理。因此,需要在 XAML groupstyle 中声明处理程序:
<ListBox.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
<Style TargetType="{x:Type GroupItem}">
<EventSetter Event="GroupItem.Drop" Handler="GroupItemDrop"/>
<EventSetter Event="GroupItem.PreviewMouseMove" Handler="ListBoxItemPreviewMouseMove"/>
Run Code Online (Sandbox Code Playgroud)
它有效,这里是完整的工作代码:http://1drv.ms/1FhBZwr
祝您获得最好的代码
| 归档时间: |
|
| 查看次数: |
301 次 |
| 最近记录: |