バグを生成するには、 で任意の項目を選択しますTopDataGrid
。その結果、アイテムのコレクションが にロードされBottomDataGrid
ます。このコレクションはName
、私が指定したプロパティでソートされています! 次に、 で他の項目を選択しますTopDataGrid
。その結果ItemsSource
、BottomDataGrid
がリロードされます。そして今、コレクションは未分類です!コレクションは、コードで指定したとおりに見えます。さらに、_customerView
デバッガーで確認すると、ソートされたコレクションが表示されます。
andの代わりに UI 自体を更新するように明示的にコマンドを実行するためList
にOrderBy
andを使用できることはわかっています。しかし、これは適切なアプローチではないと思います。INotifyPropertyChanged
ObservableCollection
ICollectionView
Win 7、.Net 4.0。コピーして貼り付けるだけです。
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<DataGrid Grid.Row="0"
AutoGenerateColumns="False"
ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
SelectionMode="Single"
SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<DataGrid Grid.Row="1"
AutoGenerateColumns="False"
ItemsSource="{Binding SelectedItem.MyCollectionView}"
CanUserAddRows="False"
CanUserDeleteRows="False"
CanUserResizeRows="False"
CanUserSortColumns="False"
SelectionMode="Single"
SelectionUnit="FullRow">
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}"></DataGridTextColumn>
<DataGridTextColumn Binding="{Binding Index}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<Grid Grid.Row="2">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBox Grid.Column="0"
Text="{Binding Name1}"></TextBox>
<TextBox Grid.Column="1"
Text="{Binding Index}"></TextBox>
</Grid>
</Grid>
コード
public class TopGridItem
{
private ObservableCollection<BottomGridItem> _collection;
public ObservableCollection<BottomGridItem> Collection
{
get { return _collection; }
}
public String Name { get; set; }
public ICollectionView MyCollectionView
{
get
{
ICollectionView _customerView = CollectionViewSource.GetDefaultView(Collection);
_customerView.SortDescriptions.Add(new SortDescription("Name", ListSortDirection.Ascending));
return _customerView;
}
}
public TopGridItem()
{
_collection = new ObservableCollection<BottomGridItem>();
_collection.Add(new BottomGridItem { Name = "bbbbbb" });
_collection.Add(new BottomGridItem { Name = "aaaaa" });
_collection.Add(new BottomGridItem { Name = "aaaaa" });
_collection.Add(new BottomGridItem { Name = "ccccc" });
_collection.Add(new BottomGridItem { Name = "dddddd" });
}
}
public class BottomGridItem
{
public String Name { get; set; }
public String Index { get; set; }
}
/// <summary>
/// Логика взаимодействия для NewWindow.xaml
/// </summary>
public partial class ProgressWindow : INotifyPropertyChanged
{
public TopGridItem _selectedItem;
public String Name1 { get; set; }
public String Index { get; set; }
public ObservableCollection<TopGridItem> Items { get; set; }
public TopGridItem SelectedItem
{
get { return _selectedItem; }
set
{
_selectedItem = value;
OnPropertyChanged("SelectedItem");
}
}
public ProgressWindow()
{
InitializeComponent();
DataContext = this;
Items = new ObservableCollection<TopGridItem>();
Items.Add(new TopGridItem {Name = "One"});
Items.Add(new TopGridItem {Name = "Two"});
Items.Add(new TopGridItem {Name = "Three"});
}
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
アップデート
private void ClearSortDescriptionsOnItemsSourceChange()
{
this.Items.SortDescriptions.Clear();
this._sortingStarted = false;
List<int> descriptionIndices = this.GroupingSortDescriptionIndices;
if (descriptionIndices != null)
descriptionIndices.Clear();
foreach (DataGridColumn dataGridColumn in (Collection<DataGridColumn>) this.Columns)
dataGridColumn.SortDirection = new ListSortDirection?();
}
private static object OnCoerceItemsSourceProperty(DependencyObject d, object baseValue)
{
DataGrid dataGrid = (DataGrid) d;
if (baseValue != dataGrid._cachedItemsSource && dataGrid._cachedItemsSource != null)
dataGrid.ClearSortDescriptionsOnItemsSourceChange();
return baseValue;
}
ClearSortDescriptionsOnItemsSourceChange
メソッドの並べ替えがクリアされ、再指定されないようです。これが問題だと思います。