2016-06-24 5 views
0

同様の質問がありますが、間違っています。XAMLでDataContextが正しく設定されている

は私が持っている:

<DataGrid Name="gridStudents" ItemsSource="{Binding Source={StaticResource cvsStudentList}}" 
      Margin="2" 
      Height="250" 
      AutoGenerateColumns="False" IsReadOnly="True"> 

しかし、私はそれが正しいもはやであるとは思わない:

<Window.Resources> 
    <local:StudentList x:Key="StudentList" /> 
    <local:InverseBooleanConverter x:Key="InverseBooleanConverter" /> 
    <local:StudentAssignmentToStudentAssignmentLookup x:Key="LookupHistoryConvertor" /> 
    <CollectionViewSource x:Key="cvsStudentList" Source="{StaticResource StudentList}" Filter="CollectionViewSource_Filter"/> 

</Window.Resources> 

<Window.DataContext> 
    <local:OCLMEditorModel/> 
</Window.DataContext> 

Futherダウン私のマークアップで私はDataGridを持っています。私のOCLMEditorModelオブジェクトは、公開プロパティStudentListと呼ばれています。私がこの権利を理解している場合、現時点では私のウィンドウはOCLMEditorModelというインスタンスに関連付けられています。ただし、後続のDataGridは、の別の CollectionViewSourceのインスタンスに関連付けられています。

私は混乱しています。ガイダンスをありがとう。

答えて

2

があなたの

<CollectionViewSource x:Key="cvsStudentList" Source="{StaticResource StudentList}" Filter="CollectionViewSource_Filter"/> 
を変更しようとすると...次のように DataContextを設定することができ、 StudentListプロパティにバインドしていません

<CollectionViewSource x:Key="cvsStudentList" Source="{Binding StudentList}" Filter="CollectionViewSource_Filter"/> 

ウィンドウのインスタンス化時に、DataContextが最初にOCLMEditorModelのインスタンスに設定されていることは間違いありません。つまり、CollectionViewSourceリソースは、直接バインドを介してWindowのDataContextからStudentListプロパティを取得できる必要があります。

はい、xamlで行っていることは、DataGridのItemsSourceをCollectionViewSourceの別のインスタンスにバインドすることです。 しかし、あなたも私が何をしたいとは思わない、あなたのWindow.Resourcesで定義されてStudentList<local:StudentList x:Key="StudentList" /> )の明確なインスタンスにそのCollectionViewSourceインスタンスを結合しています。私が上記で提案した変更は、あなたのCollectionViewSourceOCLMEditorModelのStudentListプロパティにバインドさせるでしょう。

+0

ありがとうございます。私はこれを試して、あなたに戻ってきます。 –

+0

これを行うと、StudentListがWPFでサポートされていないことがわかります。 –

+0

私はバインディングワードを忘れていました。しかし、私のグリッドはデータを表示しません。 –

1

なぜあなたはちょうど

<DataGrid Name="gridStudents" ItemsSource="{Binding StudentList}" ... /> 
+0

DataGridのItemSourceにcollectionviewsourceをバインドします。プロパティに直接バインドしないでください – Rowbear

+0

@RowbearフィルタリングのためにCVSを使用しています。 –

0

のようにあなたが

<Window.Resources> 
      <local:OCLMEditorModel x:Key="MyViewModel"/> 
     </Window.Resources> 
    <DataGrid DataContext="{StaticResource MyViewModel}" Name="gridStudents" ItemsSource="{Binding Source={StaticResource cvsStudentList}}" 
       Margin="2" 
       Height="250" 
       AutoGenerateColumns="False" IsReadOnly="True"> 
+0

ありがとうございます。しかしcvsはどうですか? OCLMEditor Studentリスト・プロパティにバインドする必要があります。これも変更する必要はありませんか? –

1

あなたは直接のviewmodelであなたのcollectionviewsourceを定義し、彼が並べ替えしたいので、彼はそうcollectionviewsourceを使用しています

public class StudentViewModel 
{ 
    public ObservableCollection<student> StudentList { get; set; } 

    public ICollectionView StudentView { get; set; } 

    public StudentViewModel() 
    { 
     StudentList= new ObservableCollection<student>(); 
     StudentView = new CollectionView(StudentList); 
     StudentView.Filter = Filter; 
     StudentView.SortDescriptions.Add(new SortDescription("Name",ListSortDirection.Ascending)); 
    } 

    private bool Filter(object obj) 
    { 
     return true; 
    } 
} 

<DataGrid Name="gridStudents" ItemsSource="{Binding StudentView}" ... /> 
+0

ありがとうございます。私はそれを他の答えに基づいて働いている。私はXAMLとは対照的に、コードの背後にあるあなたのことを推測します。しかし、両方の結果は同じです。 –

+0

補足として、私はクラス 'OCLMEditorModel'を呼び出しましたが、' OCLMEditorViewModel'と呼んでいたはずです。私のソース 'XML'ファイルは' Model'オブジェクトと見なされますか? –

+0

注:xamlでcollectionviewsourceを定義し、そのフィルタをxaml.csのコードの背後にあるmvvmに違反しています。 –

関連する問題