[英]Correctly setting my DataContext in XAML
I know there are similar questions but I am getting this wrong. 我知道也有类似的问题,但是我错了。
I have: 我有:
<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 down in my markup I have a DataGrid
: 在我的标记中,我还有一个
DataGrid
:
<DataGrid Name="gridStudents" ItemsSource="{Binding Source={StaticResource cvsStudentList}}"
Margin="2"
Height="250"
AutoGenerateColumns="False" IsReadOnly="True">
But I don't think it is right anymore. 但是我不认为这是对的。 My OCLMEditorModel object has a public property called
StudentList
. 我的OCLMEditorModel对象具有一个称为
StudentList
的公共属性 。 If I am understanding this right, at the moment my window is associated with an instance of OCLMEditorModel
. 如果我理解此权利,那么目前我的窗口与
OCLMEditorModel
实例相关联。 But the subsequent DataGrid is associated with a distinct instance of the CollectionViewSource. 但是随后的DataGrid与CollectionViewSource的不同实例相关联。
So I am getting myself confused. 所以我让自己感到困惑。 Thanks for guidance.
感谢您的指导。
Trying changing your 尝试改变你的
<CollectionViewSource x:Key="cvsStudentList" Source="{StaticResource StudentList}" Filter="CollectionViewSource_Filter"/>
to 至
<CollectionViewSource x:Key="cvsStudentList" Source="{Binding StudentList}" Filter="CollectionViewSource_Filter"/>
You're correct that your DataContext is set initially to an instance of OCLMEditorModel
when the window is instantiated. 您是正确的,当实例化窗口时,DataContext最初设置为
OCLMEditorModel
的实例。 That should mean that your CollectionViewSource resource should be able to grab the StudentList
property from the Window's DataContext through a direct binding. 这意味着您的CollectionViewSource资源应该能够通过直接绑定从Window的DataContext中获取
StudentList
属性。
Yes, what you're doing in your xaml is binding your DataGrid's ItemsSource to a distinct instance of the CollectionViewSource. 是的,您在xaml中所做的是将DataGrid的ItemsSource绑定到CollectionViewSource的不同实例。 However , you're also binding that
CollectionViewSource
instance to a distinct instance of a StudentList
defined in your Window.Resources ( <local:StudentList x:Key="StudentList" />
), which I don't think is what you want. 但是 ,您还将该
CollectionViewSource
实例绑定到Window.Resources( <local:StudentList x:Key="StudentList" />
)中定义的StudentList
的不同实例,我认为这不是您想要的。 The change I suggested above will make your CollectionViewSource
bind to the OCLMEditorModel
's StudentList property instead. 我上面建议的更改将使您的
CollectionViewSource
绑定到OCLMEditorModel
的StudentList属性。
您为什么不只绑定到StudentList
属性,例如
<DataGrid Name="gridStudents" ItemsSource="{Binding StudentList}" ... />
You directly define your collectionviewsource in the viewmodel and bind collectionviewsource to DataGrid ItemSource 您可以在视图模型中直接定义collectionviewsource并将collectionviewsource绑定到DataGrid ItemSource
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}" ... />
You can set DataContext
to the following way... 您可以将
DataContext
设置为以下方式...
<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">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.