2

これは私の最初のWPFプロジェクトですので、私と一緒に裸にしてください。ここでDataGrid CollectionViewSourcesをソートするためのWPF ListCollectionViewの作成方法

は私CollectionViewSourcesです:

<CollectionViewSource x:Key="topLevelAssysViewSource" d:DesignSource="{d:DesignInstance my:TopLevelAssy, CreateList=True}" /> 
<CollectionViewSource x:Key="topLevelAssysRefPartNumsViewSource" Source="{Binding Path=RefPartNums, Source={StaticResource topLevelAssysViewSource}}" /> 
<CollectionViewSource x:Key="topLevelAssysRefPartNumsRefPartNumBomsViewSource" Source="{Binding Path=RefPartNumBoms, Source={StaticResource topLevelAssysRefPartNumsViewSource}}" /> 

私は現在、相互にデータを送り、次のコントロールがあります。

私の窓のためのDataContextは、私のコントロールのすべてのGridハウジングを介して供給されます。

<Grid DataContext="{StaticResource topLevelAssysViewSource}"> 

コンボボックス:

<ComboBox DisplayMemberPath="TopLevelAssyNum" Height="23" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="12,12,0,0" Name="topLevelAssysComboBox" SelectedValuePath="TopLevelAssyID" VerticalAlignment="Top" Width="120" /> 

ListBoxコントロール:

<ListBox DisplayMemberPath="RefPartNum1" Height="744" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource topLevelAssysRefPartNumsViewSource}}" Margin="12,41,0,0" Name="refPartNumsListBox" SelectedValuePath="RefPartNumID" VerticalAlignment="Top" Width="120" /> 

は最後に、私はソート可能な作るしようとしているデータグリッド:(今のちょうど1列):

<DataGrid CanUserSortColumns="true" AutoGenerateColumns="False" EnableRowVirtualization="True" HorizontalAlignment="Left" ItemsSource="{Binding Source={StaticResource topLevelAssysRefPartNumsRefPartNumBomsViewSource}}" Margin="6,6,0,1" Name="refPartNumBomsDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Width="707"> 
    <DataGrid.Columns > 
     <DataGridTextColumn x:Name="cageCodeColumn" Binding="{Binding Path=CageCode}" Header="CageCode" Width="45" /> 
     <DataGridTextColumn x:Name="partNumColumn" Binding="{Binding Path=PartNum}" Header="PartNum" Width="165" SortDirection="Ascending" /> 
    </DataGrid.Columns> 
</DataGrid> 

私の正確なコードこれまでのところは、次のとおりです。

public partial class MainWindow : Window 
{ 
    racr_dbEntities racr_dbEntities = new racr_dbEntities(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private System.Data.Objects.ObjectQuery<TopLevelAssy> GetTopLevelAssysQuery(racr_dbEntities racr_dbEntities) 
    { 
     // Auto generated code 

     System.Data.Objects.ObjectQuery<racr_dbInterface.TopLevelAssy> topLevelAssysQuery = racr_dbEntities.TopLevelAssys; 
     // Update the query to include RefPartNums data in TopLevelAssys. You can modify this code as needed. 
     topLevelAssysQuery = topLevelAssysQuery.Include("RefPartNums"); 
     // Update the query to include RefPartNumBoms data in TopLevelAssys. You can modify this code as needed. 
     topLevelAssysQuery = topLevelAssysQuery.Include("RefPartNums.RefPartNumBoms"); 
     // Returns an ObjectQuery. 
     return topLevelAssysQuery; 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
     // Load data into TopLevelAssys. You can modify this code as needed. 
     CollectionViewSource topLevelAssysViewSource = ((CollectionViewSource)(this.FindResource("topLevelAssysViewSource"))); 
     ObjectQuery<racr_dbInterface.TopLevelAssy> topLevelAssysQuery = this.GetTopLevelAssysQuery(racr_dbEntities); 
     topLevelAssysViewSource.Source = topLevelAssysQuery.Execute(MergeOption.AppendOnly); 

     ListCollectionView topLevelAssyView = CollectionViewSource.GetDefaultView(CollectionViewSource.CollectionViewTypeProperty) as ListCollectionView; 
     topLevelAssyView.SortDescriptions.Add(new SortDescription("PartNum", ListSortDirection.Descending)); 
    } 

私は、CollectionViewSに含まれるソートプロパティを処理するためにListCollectionViewsを作成することの重要性を理解しています私のブログは、Bea Stollnitz's blogから入手しました。

ただし、エラーメッセージNull参照Exception未処理: "オブジェクト参照がオブジェクトのインスタンスに設定されていません"が表示されます。

どうすればこの問題を解決できますか? ListCollectionViewをさらに定義する必要がありますか、おそらくICollectionViewを確立する必要がありますか?私のPartNum列には、数字と時には文字で始まる部品番号が含まれています。標準的なsortdirectionsが適用されますか?

答えて

0

例外の完全なスタックトレースを提供するか、この例外をスローするサンプルの行数以上を指定してください。

あなたがこれまで提供してきたことから、私は、エラーの原因が

ListCollectionView topLevelAssyView = CollectionViewSource.GetDefaultView(CollectionViewSource.CollectionViewTypeProperty) as ListCollectionView; 

あなたはEntity Frameworkのを使用している場合は、ObjectQueryの結果のデフォルトのビューは、したがって、とNullReferenceExceptionたListCollectionViewではないだろうと思います。

ObjectQuery/EntityCollectionをCollectionViewSourceのソースとして使用してソートするには、ソートをサポートする別のコンテナにラップする必要があります(CRUDを実行する場合は、ソースEntityCollectionの代わりにそのコンテナを使用します)。

ObservableCollection<TopLevelAssy> observableCollection = new ObservableCollection(topLevelAssysQuery.Execute(MergeOption.AppendOnly)); 
((ISupportInitialize)topLevelAssysViewSource).BeginInit(); 
topLevelAssysViewSource.CollectionViewType = typeof(ListCollectionView); 
topLevelAssysViewSource.Source = observableCollection; 
topLevelAssysViewSource.SortDescriptions.Add(new SortDescription("CageCode", ListSortDirection.Ascending)); 
((ISupportInitialize)topLevelAssysViewSource).EndInit(); 

をそして、あなたがCollectionViewSource.Viewプロパティを参照するためにバインディングを変更:

例えば、これらの線に沿って何かをしようと

ItemsSource="{Binding Source={StaticResource topLevelAssysViewSource}, Path=View}" 

追加読書:http://blog.nicktown.info/2008/12/10/using-a-collectionviewsource-to-display-a-sorted-entitycollection.aspx

+0

が応答をありがとう、サーフェン - 本当に感謝しています。エラーメッセージがListCollectionViewが定義されている行に表示されるという点では正しいです。私はObservableCollectionを作成し、あなたの言ったようにItemsSourceを変更しようとしましたが、それは私の頭の中で少しだったので、編集したコードを正常に実行することができませんでした。私はあなたが言ったことを見直し、私の質問のより洞察的な再投稿を投稿しようとします。 –

+0

ようこそ。ところで、XAMLでインスタンス化した後にCollectionViewTypeを変更することができる場合は、この割り当てをXAMLに移動することができます。しかし、私はCollectionViewSourcesに関するいくつかの問題を抱えていましたが、やがてコードでそれらを作成して維持しました。 – surfen

+0

うーん...まだ解決策はありません。私は本当に私が必要と思う1.私のcollectionViewSourcesのobservablesCollectionを確立するか、または2. ICollectionViewを作成します。これは本当に実現するよりも簡単に思えました。 –

関連する問題