【问题标题】:Correctly setting my DataContext in XAML在 XAML 中正确设置我的 DataContext
【发布时间】:2016-06-24 18:16:03
【问题描述】:

我知道有类似的问题,但我弄错了。

我有:

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

在我的标记中,我有一个DataGrid:

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

但我认为它不再正确了。我的 OCLMEditorModel 对象有一个名为StudentList 的公共属性。如果我理解这一点,目前我的窗口与OCLMEditorModel 的实例相关联。但随后的 DataGrid 与 CollectionViewSource 的 distinct 实例相关联。

所以我让自己感到困惑。感谢您的指导。

【问题讨论】:

    标签: c# wpf xaml datacontext


    【解决方案1】:

    尝试改变你的

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

    到

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

    在实例化窗口时,您的 DataContext 最初设置为 OCLMEditorModel 的实例是正确的。这应该意味着您的 CollectionViewSource 资源应该能够通过直接绑定从 Window 的 DataContext 中获取 StudentList 属性。

    是的,您在 xaml 中所做的是将 DataGrid 的 ItemsSource 绑定到 CollectionViewSource 的不同实例。 然而,您还将CollectionViewSource 实例绑定到您的Window.Resources (&lt;local:StudentList x:Key="StudentList" /&gt;) 中定义的StudentList 的不同 实例 ),我认为这不是你想要的。我上面建议的更改将使您的 CollectionViewSource 绑定到 OCLMEditorModel 的 StudentList 属性。

    【讨论】:

    • 谢谢。我会试试这个并回复你。
    • 如果我这样做,它会告诉我 WPF 不支持 StudentList。
    • 我忘记了绑定词。但是现在我的网格没有显示任何数据。
    • 已排序。我忘记了我已将我的财产重命名为学生。
    • @AndrewTruckle 绝对是我最不喜欢 WPF 的东西! :D
    【解决方案2】:

    你为什么不直接绑定StudentList属性,比如

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

    【讨论】:

    • 他可能会使用 collectionviewsource,因为他想进行排序。直接绑定到属性是不行的
    • @Rowbear 由于过滤,我正在使用 cvs。
    【解决方案3】:

    你直接在viewmodel中定义你的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}" ... />
    

    【讨论】:

    • 谢谢。我让它根据另一个答案工作。我猜你是在代码后面而不是在 XAML 中。但两者都以相同的结果结束。
    • 作为旁注,我给我的班级打电话OCLMEditorModel,但听起来我应该叫它OCLMEditorViewModel?而我的源XML 文件将被视为Model 对象?
    • 注意:您在 xaml 中定义了您的 collectionviewsource,并在 xaml.cs 中的代码中定义了它的过滤器,这违反了 mvvm。
    • 对不起?在我的问题代码中,过滤器处理程序是在 XAML 中指定的。
    【解决方案4】:

    您可以将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">
    

    【讨论】:

    • 谢谢。但是 cvs 呢?我们需要绑定到 OCLMEditor 学生列表属性。这也不需要改变吗?
    猜你喜欢
    • 1970-01-01
    • 2014-07-06
    • 1970-01-01
    • 2013-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多