WPF Binding to DataGrid

本文关键字:DataGrid to Binding WPF | 更新日期: 2023-09-27 18:13:13

我是WPF的新手,只是想让我的头围绕绑定,我可以让数据网格绑定,但我必须以某种方式来做。下面是我的类,如果我像所示的那样在构造函数中获取数据,它就能工作。我将数据上下文绑定到Attorney类,并将项目源绑定到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();
        }
    }
}

但是,假设我不想在构造函数中调用getAtty(),而是在Xaml文件后面的代码中,像这样,

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        var odata = new Data();
        odata.getAtty();
    }

由于某种原因,数据网格没有填充?我知道我可以在第一个代码帖子的构造函数中做到这一点,但如果我想单独调用它,而不是当我有一个新的实例时,我不能。我做错了什么?

编辑:这里是我的大部分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>

WPF Binding to DataGrid

使用ObservableCollection<Attorneys>而不是List,以便通知DataGrid更改。

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();
        }
    }
}

在你设置DataContext的XAML中,给那个实例一个名字。

<DataGrid.DataContext>
    <Models:Data x:Name="dataContext" />
<DataGrid.DataContext>

然后在Window's Loaded事件处理程序中,引用该实例来调用getAtty()

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    dataContext.getAtty();
}