C# WPF绑定到DataGrid

C# WPF绑定到DataGrid,c#,wpf,C#,Wpf,我是WPF新手,只是想了解一下绑定,我可以让datagrid进行绑定,但我必须以某种方式进行绑定。这是我的类,如果我在构造函数中抓取数据(如图所示),它就会工作。我将datacontext绑定到Drawners类,并将项源绑定到AttyList属性。我的INotifyChanged也在工作 public class Data : CommonBase { public Data() { getAtty(); } private List<A

我是WPF新手,只是想了解一下绑定,我可以让datagrid进行绑定,但我必须以某种方式进行绑定。这是我的类,如果我在构造函数中抓取数据(如图所示),它就会工作。我将datacontext绑定到Drawners类,并将项源绑定到AttyList属性。我的INotifyChanged也在工作

 public class Data : CommonBase
{
    public Data()
    {
        getAtty();
    }
    private List<Attorneys> _AttyList;

    public List<Attorneys> AttyList
    {
        get { return _AttyList; }
        set
        {
            if(value != this._AttyList)
            {
                _AttyList = value;
                NotifyPropertyChanged("AttyList");
            }

        }
    }

    public void getAtty()
    {
        AttyList = new List<Attorneys>();
        using (var context = new Context())
        {
            AttyList = context.Attorney.ToList();
        }
    }

}
不管出于什么原因,datagrid没有填充?我知道我可以在第一个代码帖子的构造函数中完成,但是如果我想单独调用它,而不是在我有一个新实例时调用它,我就做不到。我做错了什么

编辑:以下是我的大部分XAML:

  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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    xmlns:local="clr-namespace:testWPF"
    xmlns:Models="clr-namespace:testWPF.Models" x:Class="testWPF.MainWindow"
    mc:Ignorable="d"

    Title="MainWindow" Height="350" Width="525" WindowState="Maximized" Loaded="Window_Loaded">
<Grid>
    <Grid.Background>
        <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FF3E0213" Offset="0.116"/>
            <GradientStop Color="White" Offset="1"/>
            <GradientStop Color="#FF970202" Offset="0.983"/>
        </LinearGradientBrush>
    </Grid.Background>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="35*"/>
        <ColumnDefinition Width="12*"/>
    </Grid.ColumnDefinitions>
    <DataGrid x:Name="dgvAllAttorneys" HorizontalAlignment="Left" Margin="84,26,0,0" MouseDoubleClick="dgvAllAttorneys_MouseDoubleClick" VerticalAlignment="Top" Height="154" Width="208" ItemsSource="{Binding AttyList}" AutoGenerateColumns="False" SelectionMode="Single" IsReadOnly="True" Grid.ColumnSpan="1">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding FirstName}" ClipboardContentBinding="{x:Null}" Header="FirstName"/>
            <DataGridTextColumn Binding="{Binding FullName}" ClipboardContentBinding="{x:Null}" Header="LastName" Width="*"/>
        </DataGrid.Columns>
        <DataGrid.DataContext>
            <Models:Data/>
        </DataGrid.DataContext>
    </DataGrid>

</Grid>
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:mc=”http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local=“clr命名空间:testWPF”
xmlns:Models=“clr命名空间:testWPF.Models”x:Class=“testWPF.MainWindow”
mc:Ignorable=“d”
Title=“MainWindow”Height=“350”Width=“525”WindowState=“Maximized”Loaded=“Window\u Loaded”>

使用一个
可观察集合
而不是列表,以便将更改通知给数据网格

public class Data : CommonBase
{
    public Data()
    {
        getAtty();
    }
    private ObservableCollection<Attorneys> _AttyList;

    public ObservableCollection<Attorneys> AttyList
    {
        get { return _AttyList; }
        set
        {
            if(value != this._AttyList)
            {
                _AttyList = value;
                NotifyPropertyChanged("AttyList");
            }
        }
    }

    public void getAtty()
    {
        AttyList = new ObservableCollection<Attorneys>();
        using (var context = new Context())
        {
            AttyList = context.Attorney.ToList();
        }
    }
}

如果你也发布了你的xaml,这会有所帮助。正如其他人提到的,使用
observedcollection
而不是列表。另外,您如何将网格绑定到集合?我添加了我的XAML,没有太多。我尝试了可观察的收集。如果没有填充网格,仍然不会。如果我从数据类构造函数调用getAtty(),网格工作正常。您在哪里设置数据上下文。执行var odata=新数据();odata.getAtty();就像创建一个类的新实例,调用一个方法,而对它返回的内容不做任何操作。@TYY我正在XAML中设置DataContext。我这样做的原因是因为我试图填充列表/ObservableCollection。如果这是错误的,我怎么能不在构造函数中这样做呢?好的,我将attylist更改为Observable collection,然后将getatty()函数更改为this,但仍然不起作用。AttyList=新的可观察集合(列表);
public class Data : CommonBase
{
    public Data()
    {
        getAtty();
    }
    private ObservableCollection<Attorneys> _AttyList;

    public ObservableCollection<Attorneys> AttyList
    {
        get { return _AttyList; }
        set
        {
            if(value != this._AttyList)
            {
                _AttyList = value;
                NotifyPropertyChanged("AttyList");
            }
        }
    }

    public void getAtty()
    {
        AttyList = new ObservableCollection<Attorneys>();
        using (var context = new Context())
        {
            AttyList = context.Attorney.ToList();
        }
    }
}
<DataGrid.DataContext>
    <Models:Data x:Name="dataContext" />
<DataGrid.DataContext>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
    dataContext.getAtty();
}