在我的代碼我有一些項目一個ObservableCollection:
myCalenderItems = new ObservableCollection<CalendarItem>();
有列表排序,我創建了一個CollectionViewSource,我必然在代碼中一個DataGrid的背後:
ICollectionView mySortedCalenderItems = CollectionViewSource.GetDefaultView(myCalenderItems);
mySortedCalenderItems.SortDescriptions.Add(new SortDescription("Day", ListSortDirection.Ascending));
mySortedCalenderItems.SortDescriptions.Add(new SortDescription("Time", ListSortDirection.Ascending));
MainGrid.DataContext = mySortedCalenderItems;
CalendarDataGrid.ItemsSource = mySortedCalenderItems;
DataGrid的XAML看起來如下:
<DataGrid Name="CalendarDataGrid" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" CanUserSortColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Day" ItemsSource="{Binding Source={StaticResource WeekDayEnum}}" Width="*" SelectedValueBinding="{Binding Day, Mode=TwoWay}"/>
<DataGridTextColumn Header="Time" Binding="{Binding Time}" Width="*" IsReadOnly="False"/>
<DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="*" IsReadOnly="False"/>
</DataGrid.Columns>
<DataGrid.GroupStyle>
<GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle}">
<GroupStyle.Panel>
<ItemsPanelTemplate>
<DataGridRowsPresenter/>
</ItemsPanelTemplate>
</GroupStyle.Panel>
</GroupStyle>
</DataGrid.GroupStyle>
</DataGrid>
而且我也希望能夠編輯數據網格旁邊使用其他控件的值,e.g
<ComboBox Text="{Binding Path=Day}" IsSynchronizedWithCurrentItem="True"/>
這裏是在那裏我有我的問題。當更改數據網格中的日期時,排序工作正常,但通過組合框控件更改時,它在數據網格中發生更改,但不會重新排序。 (在改變之前修改的項目的另一個屬性時重新排序)
你有什麼想法嗎?
問候
PS: 整套XAML
<UserControl
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:System="clr-namespace:System;assembly=mscorlib"
xmlns:local="clr-namespace:MyProject"
mc:Ignorable="d"
d:DesignHeight="520" d:DesignWidth="840">
<UserControl.Resources>
<ObjectDataProvider x:Key="WeekDayEnum" MethodName="GetValues"
ObjectType="{x:Type System:Enum}">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="local:WeekDay"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
</UserControl.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid x:Name="MainGrid" Grid.Row="0">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
<RowDefinition Height="*"/>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="240"/>
<ColumnDefinition Width="180"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Weekly" HorizontalAlignment="Left" VerticalAlignment="Center"/>
<DataGrid x:Name="CalendarDataGrid" Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" AutoGenerateColumns="False" CanUserSortColumns="False">
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Day" ItemsSource="{Binding Source={StaticResource WeekDayEnum}}" Width="*" SelectedValueBinding="{Binding Day, Mode=TwoWay}" />
<DataGridTextColumn Header="Time" Binding="{Binding Time}" Width="*" IsReadOnly="False"/>
<DataGridTextColumn Header="Value" Binding="{Binding Value}" Width="*" IsReadOnly="False"/>
</DataGrid.Columns>
</DataGrid>
<Grid Grid.Row="1" Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="70"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Content="Day" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20,0,0,0"/>
<Label Grid.Row="1" Content="Time" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20,0,0,0"/>
<Label Grid.Row="2" Content="Value" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20,0,0,0"/>
<ComboBox Grid.Row="0" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center" Width="100" Text="{Binding Path=Day}" IsSynchronizedWithCurrentItem="True"/>
<TextBox Grid.Row="1" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Width="100" Text="{Binding Path=Time, Mode=TwoWay}"/>
<TextBox Grid.Row="2" Grid.Column="1" HorizontalAlignment="Left" VerticalAlignment="Center" Height="23" TextWrapping="Wrap" Width="100" Text="{Binding Path=Value, Mode=TwoWay}"/>
</Grid>
</Grid>
</Grid>
嘗試調用'mySortedCalenderItems。刷新();'在'ComboBox' –
的'SelectionChanged'事件如果MainGrid是數據網格的父沒有必要爲 CalendarDataGrid.ItemsSource = mySortedCalenderItems;在SelectionChanged事件 – Maximus
刷新電話給我這個錯誤: 「刷新」一個AddNew或EditItem交易過程中是不允許的。 – b2f