2

アプリケーションのクライアント/サーバーに問題があります。私はMVVMパターンで作業しています。私の見解では、これらのコード行で ViewModel にバインドされている がありますDataGridICollection

public ICollectionView Customers
    {
        get
        {
            return _customers;
        }
        set
        {
            _customers= value;
            RaisePropertyChanged("Customers");
        }
    }

_customers = CollectionViewSource.GetDefaultView(Manager.Instance.Customers());
_customers .SortDescriptions.Add(new SortDescription("CreationDate", ListSortDirection.Descending));

最初は、次の 2 行が正常に機能します。my DataGridhas my list of customers and the sort is ok.

しかし、サーバーが私の顧客リストを更新するとき、私は自分のコレクションを更新したいと思いDataGridます。それで、新しい顧客リストの通知を受け取りました。この通知を受け取ったとき、コレクションを次の行で更新します。

 Customers = CollectionViewSource.GetDefaultView(e.CustomersInfo);
_customers.SortDescriptions.Clear();
_customers .SortDescriptions.Add(new SortDescription("CreationDate", ListSortDirection.Descending));
Customers.Refresh();

ここではDataGrid、適切なデータで適切に更新されていますが、最初は顧客のリストが CreationDate で並べ替えられているため、並べ替えは更新されていませんが、更新後は CustomersName で並べ替えられています。

ここで、私の XAML コード:

<DataGrid AutoGenerateColumns="false" ItemsSource="{Binding Customers, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" Name="dtgEventInfo" SelectedItem="{Binding SelectedCustomers, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" ItemContainerStyle="{StaticResource ItemContStyle}" IsEnabled="True" IsReadOnly="True" Margin="0,0,330,0" GridLinesVisibility="None" SelectionMode="Single">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding Name, Mode=TwoWay}" Header="Name" MinWidth="40" Width="Auto"/>
                    <DataGridTextColumn Binding="{Binding CreationDate, Mode=TwoWay}" Header="Date" MinWidth="50" Width="Auto"/>
                </DataGrid.Columns>
</DataGrid>

私を助けてくれるアイデアはありますか?いくつかの調査の後、私はボットが解決策を見つけました...

4

5 に答える 5

0

必要な動作が必要な場合は、次のことを行います。

  • 私は自分のタイプONCEのObservableCollectionを作成します。_mysource
  • 私は ICollectionView ONCE を作成します。_私の見解
  • Clear/Add/Remove を使用して _mysource を更新します
  • _myview.Refresh() で、並べ替え/グループ化/フィルタリングが適用されます

これはあなたのために働くでしょう

 this._mysource.Clear();
 this._mysource.AddRange(e.CustomersInfo);
 _myview.Refresh();
于 2013-06-24T10:25:58.933 に答える
0

Visual Studio でいくつかの検索と多くのデバッグ モードを行った後、問題が見つかったと思います。私のマネージャーManager.csで、通知を受け取ったとき、私はこれをしました:

 ObservableCollection<CustomerInfo> collection = new ObservableCollection<CustomerInfo>(e.CustomersInfoList); 
CustomerListViewModel.Instance.Customers = collection; 

したがって、この新しいインスタンス化はおそらく私の問題を引き起こす可能性があります。なぜなら、この同じコレクションで、いくつかのフィルターを実現し、フィルターメソッドの後にソートがOKだからです!

では、今何かアイデアはありますか?

于 2013-06-25T07:21:37.070 に答える