这就是我想要的:如果我将 ICollectionview 绑定到 DataGrid,我不想在我的 Viewmodel 中丢失 SortDescription。
我创建了一个小示例项目来了解我的意思。在我的项目中,我只是使用 Usercontrol 在 DataGrid 中显示我的数据。如果我这样做,当 UserControl 卸载时, SortDescritpion 就消失了,因为 ItemsSource 设置为 null。如果我使用 TemplateSelector 来显示我的 UserControl,SortDescription 并没有消失,并且 ItemsSource 在卸载时没有设置为 null。问题是,为什么会有这些不同的行为?两种行为中的一种是错误吗?
顺便提一句。我使用 .Net 4.5.1 但安装了 4.6.1 并且 system.Windows.Interactivity 4.0.0.0
主窗口.xaml
<Window x:Class="DataGridICollectionView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DataGridICollectionView"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<DataTemplate DataType="{x:Type local:ViewmodelListe}">
<local:MyViewUc/>
</DataTemplate>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<ToolBar Grid.Row="0">
<Button Content="SetWorkspace MyView" Click="Button_Click"/>
<Button Content="SetWorkspace Other" Click="Button_Click_1"/>
</ToolBar>
<ContentPresenter Grid.Row="1" Content="{Binding Workspace}"/>
</Grid>
</Window>
主窗口.xaml.cs
namespace DataGridICollectionView
{
/// <summary>
/// Interaktionslogik für MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
private object _workspace;
public MainWindow()
{
InitializeComponent();
MyViewVm = new ViewmodelListe();
DataContext = this;
}
public ViewmodelListe MyViewVm { get; set; }
public object Workspace
{
get { return _workspace; }
set
{
_workspace = value;
OnPropertyChanged();
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Workspace = MyViewVm;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Workspace = "Other";
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ViewmodelListe : INotifyPropertyChanged
{
public ViewmodelListe()
{
Persons = new ObservableCollection<Person>();
MyView = CollectionViewSource.GetDefaultView(Persons);
Persons.Add(new Person() {FirstName = "P1", LastName = "L1"});
Persons.Add(new Person() {FirstName = "P2", LastName = "L2"});
Persons.Add(new Person() {FirstName = "P3", LastName = "L3"});
}
public ObservableCollection<Person> Persons { get; private set; }
public ICollectionView MyView { get; private set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class Person : INotifyPropertyChanged
{
private string _firstName;
private string _lastName;
public string FirstName
{
get { return _firstName; }
set
{
_firstName = value;
OnPropertyChanged();
}
}
public string LastName
{
get { return _lastName; }
set
{
_lastName = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class TestBehavior : Behavior<DataGrid>
{
protected override void OnAttached()
{
base.OnAttached();
AssociatedObject.Unloaded += AssociatedObjectUnloaded;
}
private void AssociatedObjectUnloaded(object sender, RoutedEventArgs e)
{
//look at this in Debug Mode, its NULL if you dont use the TemplateSelector
var itemssource = AssociatedObject.ItemsSource;
}
protected override void OnDetaching()
{
base.OnDetaching();
AssociatedObject.Unloaded -= AssociatedObjectUnloaded;
}
}
}
MyGridControl.xaml
<UserControl x:Class="DataGridICollectionView.MyGridControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DataGridICollectionView"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<DataGrid ItemsSource="{Binding MyView}" AutoGenerateColumns="True">
<i:Interaction.Behaviors>
<local:TestBehavior/>
</i:Interaction.Behaviors>
</DataGrid>
</Grid>
</UserControl>
MyViewUc.xaml
<UserControl x:Class="DataGridICollectionView.MyViewUc"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DataGridICollectionView"
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<UserControl.Resources>
<DataTemplate x:Key="MyViewCrap">
<local:MyGridControl/>
</DataTemplate>
<local:MyTemplateSelector x:Key="Selector" GridView="{StaticResource MyViewCrap}" />
</UserControl.Resources>
<Grid>
<!--When using Contentcontrol with TemplateSelector- ItemsSource is NOT set to null -->
<ContentControl Content="{Binding .}" ContentTemplateSelector="{StaticResource Selector}"/>
<!--When using MyGridControl withOUT TemplateSelector- ItemsSource is set to NULL -->
<!--<local:MyGridControl/>-->
</Grid>
</UserControl>
MyViewUc.xaml.cs
namespace DataGridICollectionView
{
/// <summary>
/// Interaktionslogik für MyViewUc.xaml
/// </summary>
public partial class MyViewUc : UserControl
{
public MyViewUc()
{
InitializeComponent();
}
}
public class MyTemplateSelector : DataTemplateSelector
{
public DataTemplate GridView { get; set; }
public override DataTemplate SelectTemplate(object item, System.Windows.DependencyObject container)
{
var chooser = item as ViewmodelListe;
if (chooser == null)
{
return base.SelectTemplate(item, container);
}
return GridView;
}
}
}
编辑:我最终使用了这个
public class MyDataGrid : DataGrid
{
static MyDataGrid ()
{
ItemsSourceProperty.OverrideMetadata(typeof(MyDataGrid ),new FrameworkPropertyMetadata(null, OnPropertyChangedCallBack, OnCoerceItemsSourceProperty));
}
private ICollectionView _defaultView;
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
if(_defaultView != null)
_defaultView.CollectionChanged -= LiveSortingPropertiesOnCollectionChanged;
base.OnItemsSourceChanged(oldValue, newValue);
_defaultView = newValue as ICollectionView;
if(_defaultView != null)
_defaultView.CollectionChanged += LiveSortingPropertiesOnCollectionChanged;
}
private void LiveSortingPropertiesOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Reset)
{
foreach (var dataGridColumn in this.Columns)
{
var isSortDirectionSetFromCollectionView = false;
foreach (var sortDescription in _defaultView.SortDescriptions)
{
if (dataGridColumn.SortMemberPath == sortDescription.PropertyName)
{
dataGridColumn.SortDirection = sortDescription.Direction;
isSortDirectionSetFromCollectionView = true;
break;
}
}
if (!isSortDirectionSetFromCollectionView)
{
dataGridColumn.SortDirection = null;
}
}
}
}
private static void OnPropertyChangedCallBack(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var grd = d as MyDataGrid ;
var view = e.NewValue as ICollectionView;
if (grd == null || view == null)
return;
foreach (var dataGridColumn in grd.Columns)
{
var isSortDirectionSetFromCollectionView = false;
foreach (var sortDescription in view.SortDescriptions)
{
if (dataGridColumn.SortMemberPath == sortDescription.PropertyName)
{
dataGridColumn.SortDirection = sortDescription.Direction;
isSortDirectionSetFromCollectionView = true;
break;
}
}
//wenn die View nicht sortiert war, auch die column nicht Sortieren
if (!isSortDirectionSetFromCollectionView)
{
dataGridColumn.SortDirection = null;
}
}
}
private static object OnCoerceItemsSourceProperty(DependencyObject d, object baseValue)
{
// do nothing here - we just want to override parent behaviour.
// The _only_ thing DataGrid does here is clearing sort descriptors
return baseValue;
}
}