如何为Datagrid设置数据上下文

时间:2019-03-14 19:32:17

标签: c# wpf data-binding

我正在通过mvvm学习wpf。我有一个ViewModel通过窗口datacontext绑定到视图

Window.Xaml:

xmlns:Converter="clr-namespace:UploadFileToDB.ViewModel"

  <Window.DataContext>
        <Converter:ViewModel/>
    </Window.DataContext>

当ViewModel类中存在所有方法时,应用程序运行正常。

现在我在视图中有一个数据网格,为了填充该数据网格,我在ViewModel命名空间下创建了一个单独的类,称为populatedatagrid。

伪代码不起作用:

   namespace UploadFileToDB.ViewModel
{
    public class PopulateDatagrid
    {
        public ObservableCollection<datagridmodel> modelclasswithcombobox
        {
            get;
            set;
        }

        DataSet ds;
        public void comboboxdata()
        { //This code populate the observable collection}
        }
    }
        public class ViewModel : INotifyPropertyChanged
        {
            PopulateDatagrid populatedatagridwithobservablecollection = new PopulateDatagrid();
            public ViewModel()
            {
                populatedatagridwithobservablecollection.combobxdata();
            //Calling the above class method here to populate data. But this throws the error that BindingExpression path error that ModelClassWithcombobox property not found on object ViewModel
            }
        }
    }

由于绑定表达式错误,以上代码无法正常工作。

有效的伪代码:

   namespace UploadFileToDB.ViewModel
{
    public class ViewModel : INotifyPropertyChanged
        {
            public ObservableCollection<datagridmodel> modelclasswithcombobox
        {
            get;
            set;
        }

        DataSet ds;
        public void comboboxdata()
        { //This code populate the observable collection}
        }
            public ViewModel()
            {
                comboboxdata();
            }
        }
    }

上面的代码可以完美地工作,没有任何问题。根据我的理解,因为我仅在数据上下文上映射了ViewModel类。 xaml无法找到另一个类中存在的可观察集合。

任何人都可以帮助我实现课堂隔离并为上述情况提供解决方案

编辑:根据要求window.xaml

<Window
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Class="UploadFileToDB.Window1"
    xmlns:vm ="clr-namespace:UploadFileToDB.ViewModel" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:Converter="clr-namespace:UploadFileToDB.ViewModel"

    Title="Window1" Height="400" Width="800">
    <Window.DataContext>
        <Converter:ViewModel/>
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1500*"/>
        </Grid.ColumnDefinitions>

        <Grid.RowDefinitions>
            <RowDefinition Height="41*"/>
        </Grid.RowDefinitions>
<Tabcontrol>
<TabItem Header="Re Assign">
     <StackPanel>

                        <DataGrid  Height="150" x:Name="datagrid1" IsReadOnly="True" AutoGenerateColumns="True" CanUserAddRows="False" Width="507" Margin="150,-35,20,79" HorizontalAlignment="Left">
                            <DataGrid.Style>
                                <Style  TargetType="{x:Type DataGrid}">
                                    <Setter  Property="ItemsSource" Value="{Binding modelclasswithcombobox}"/>
                                </Style>
                            </DataGrid.Style>
                        </DataGrid>
                    </StackPanel>
</TabItem>
 </TabControl>
    </Grid>
</Window>

1 个答案:

答案 0 :(得分:0)

您可以仅绑定到属性不绑定到字段。因此,对于它,您应该使用如下代码:

public class ViewModel : INotifyPropertyChanged
{
    public PopulateDatagrid Populatedatagridwithobservablecollection
    {
        get
        {
            return _populatedatagridwithobservablecollection;
        }
        set
        {
            if (value != _populatedatagridwithobservablecollection)
            {
                _populatedatagridwithobservablecollection = value;
                NotifyPropertyChanged(nameof(Populatedatagridwithobservablecollection));
            }
        }
    }
    private PopulateDatagrid _populatedatagridwithobservablecollection = new PopulateDatagrid();;
}