5
votes

I have a Datagrid control in my WPF application and I am trying to bind that control to an ObservableCollection property in my Main Window's class. The property I'm trying to bind to is defined as:

private ObservableCollection<RequestResult> m_SentRequests = new ObservableCollection<RequestResult>();
public ObservableCollection<RequestResult> SentRequests { get { return m_SentRequests; } }

My datagrid is in a group by which has the datacontext set to the MainWindow:

<GroupBox Header="Results" Height="275" HorizontalAlignment="Stretch" Margin="0,305,0,0" Name="grpResults" VerticalAlignment="Top" Width="712" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}}">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="246" HorizontalAlignment="Stretch" Margin="6,6,6,0" Name="dgResults" VerticalAlignment="Top" ItemsSource="{Binding Path=SentRequests}" DataContext="{Binding}" IsSynchronizedWithCurrentItem="True" />
    </Grid>
</GroupBox>

The problem that I'm having is that in the properties window, after I select SentRequests as my ItemsSource, I still can't select the "Edit Property-Bound Columns" option. I get a "You must set ItemsSource before you can perform this action" dialog. I get the same error when selecting "Generate Columns" and "Remove Columns". It's as if I haven't set anything in the ItemsSource property for my Dialog.

I can set AutoGenerateColumns to true though and I see my data get bound though (however, not with the columns I want to show).

I'm very new to WPF and I'm just writing this as a quick test app for testing a windows service.

Any one know what I'm doing wrong here?

3
DataContext="{Binding}" in the DataGrid is redundant i think, because in general DataContext is being inherited.H.B.
@H.B. Thanks, I did that clean up. It didn't make a difference (not that you expected it to). Probably an artifact from me trying multiple things.Redbaran

3 Answers

3
votes

I don't believe you need the Path parameter within your itemSource. You should be able to just set the binding as ItemsSource={Binding SentRequests}

You can also bind to the grid item source in code for example if I create a dummy collection:

    public class People
{

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Age { get; set; }
    public string address { get; set; }
    public string zip { get; set; }
}

and then populate it

this.AllPeople = new ObservableCollection<People>();


        private void FillData()
    {
        People p1 = new People();
        p1.FirstName = "John";
        p1.LastName = "Doe";
        p1.Age = "24";
        p1.address = "123 Main Street";
        p1.zip = "11111";

        People p2 = new People();
        p2.FirstName = "Jane";
        p2.LastName = "Smith";
        p2.Age = "36";
        p2.address = "456 Water Street";
        p2.zip = "22222";

        People p3 = new People();
        p3.FirstName = "Larry";
        p3.LastName = "Williams";
        p3.Age = "24";
        p3.address = "785 Water Street";
        p3.zip = "33333";

        this.AllPeople.Add(p1);
        this.AllPeople.Add(p2);
        this.AllPeople.Add(p3);
    }

I could then set the items source in the mainpage contsructor or method as:

this.gridviewname.ItemsSource = "AllPeople";

2
votes

This is probably a result of some of the trickery that the designer does to render without constantly compiling (like skipping code-behind constructors). Try moving your collection to a separate class and use an instance of that as your DataContext (like an MVVM ViewModel). The other class should be able to initialize normally and provide the bound property to the designer.

1
votes

Have you tryed without the DataContext tags? Both in GroupBox and DataGrid.

EDIT

something like this:

<GroupBox Header="Results" Height="275" HorizontalAlignment="Stretch" >
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="246" HorizontalAlignment="Stretch"  Name="dgResults" VerticalAlignment="Top" ItemsSource="{Binding Path=SentRequests}" IsSynchronizedWithCurrentItem="True" />
    </Grid>
</GroupBox>