Dylan
Dylan

Reputation: 1118

WPF Binding to DataGrid

I'm new to WPF and just trying to get my head around binding, I can get the datagrid to bind, but I have to do it a certain way. Here is my class that if I grab the data in the constructor as shown, it works. I have the datacontext bound to the Attorney class, and the item source to the AttyList property. I have the INotifyChanged working also.

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

}

But, lets say I dont want to call getAtty() in the constructor, but in the code behind of the Xaml file, like so,

private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        var odata = new Data();
        odata.getAtty();

    }

For whatever reason, the datagrid doesn't populate? I know I can do it in the constructor of the first code post, but if I want to call it individually rather than when I have a new instance i can't. What am i doing wrong?

Edit: Here is most of my 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>

Upvotes: 2

Views: 284

Answers (1)

Sean Beanland
Sean Beanland

Reputation: 1118

Use an ObservableCollection<Attorneys> instead of a List so the DataGrid is notified of the changes.

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

In your XAML where you set your DataContext, give that instance a name.

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

Then in the Window's Loaded event handler, reference that instance to call getAtty()

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

Upvotes: 2

Related Questions