CollectionViewSource问题

时间:2015-07-10 12:45:05

标签: c# wpf xaml binding

我在这里遇到一个小问题:

在我的代码中,我有一个带有一些项目的ObservableCollection:

myCalenderItems = new ObservableCollection<CalendarItem>();

为了对列表进行排序,我创建了一个CollectionViewSource,我将其绑定到代码后面的数据网格中:

 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>

此外,我还希望能够使用datagrid旁边的其他控件来编辑值,例如

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

1 个答案:

答案 0 :(得分:0)

XAML中的组合框代码设置不正确。要正常工作,您需要设置define(['jquery', 'magnificpopup'], function($, magnificpopup) { return { show: function() { $.magnificPopup.open({ items: { src: 'https://www.youtube.com/watch?v=btPJPFnesV4' }, type: 'iframe', iframe: { cursor: null, titleSrc: function(item) { return 'Video title'; }, patterns: { youtube_short: { index: 'youtu.be/', id: 'youtu.be/', src: '//www.youtube.com/embed/%id%?autoplay=1' } } } }); } }); }); 属性。 (这可能需要稍微调整一下,但它会给你一个想法)。

SelectedValue

让我举一个改变另一个组合框的例子......

例如,我有以下数据类型:

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

以下是我的VM上保存为public class PriceItem { public string Name { get; set; } public int Price { get; set; } } 的值的数据列表。

Items

<强>方案 两个组合框,都绑定到Items = new List<PriceItem>() { new PriceItem() { Name = "Alpha", Price=100 }, new PriceItem() { Name = "Beta", Price=200 }, }; 数据。一个组合框在其下拉列表中显示Items,而另一个显示NamePrice组合框控制价格组合框,同时也改变价格变化。

Name

在操作中,两个组合框都是空白的。但每当我选择第一个时,它就会改变第二个。全部使用相同的数据/数据对象。

enter image description here