当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Issue with CollectionViewSource

问题描述:

I am having a small issue here:

In my Code I have an ObservableCollection with some Items:

myCalenderItems = new ObservableCollection<CalendarItem>();

To have the list sorted, I created a CollectionViewSource, which I bound to a datagrid in code behind:

 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;

The XAML for the Datagrid looks as the following:

 <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>

Further I also want to be able to edit the values using other controls beside the datagrid, e.g

<ComboBox Text="{Binding Path=Day}" IsSynchronizedWithCurrentItem="True"/>

And here is where I have my problem. When changing the day in the datagrid the sorting works, but when changing it via the combobox control it does change in the datagrid but it is not re-sorted.

(A re-sorting is done when changing another property of the before modified item)

Do you have any ideas what is wrong here?

Regards

PS:

Entire 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>

网友答案:

The combobox code in XAML is not setup properly. To work properly you will need to set the SelectedValue property. (This may need to be tweaked a bit, but it gives you the idea).

<ComboBox ItemsSource="{Binding Source={StaticResource WeekDayEnum}}" 
                       SelectedValue="{Binding Path=Day, Mode=TwoWay}" />

Let me give an example of one combo box changing another...

For example I have this data type of:

public class PriceItem
{
    public string Name { get; set; }
    public int Price { get; set; }
}

and here is the data list of values which is saved on my VM as Items.

Items = new List<PriceItem>()
{
    new PriceItem() { Name = "Alpha", Price=100 },
    new PriceItem() { Name = "Beta",  Price=200 },
};

Scenario Two combo boxes, both bound to the Items data. One combo box shows the Name in its dropdown while the other shows the Price. The Name combobox controls the price combo box, when it changes the price changes as well.

<ComboBox x:Name="comboName" ItemsSource="{Binding Items}" DisplayMemberPath="Name" />
<ComboBox x:Name="comboValue"
          ItemsSource="{Binding Items}"
          DisplayMemberPath="Price"
          SelectedValuePath="Price"
          SelectedValue="{Binding SelectedItem.Price, ElementName=comboName, Mode=OneWay}"/>

In action both combo boxes are blank. But whenever I select the first one it changes the second. All using the same data/data objects.

分享给朋友:
您可能感兴趣的文章:
随机阅读: