Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8933003
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:26:02+00:00 2026-06-15T09:26:02+00:00

I want to bind a datagrid view in a user control that is docking

  • 0

I want to bind a datagrid view in a user control that is docking to a main WPF form. However everytime I try to bind the data it must pre exist and won’t update. Is there a way to perform this in the XAML directly to know when an event is triggered to update the datagridview rather than do it in the code behind?

Partial code of XAML:

xmlns:c="clr-namespace:TestWPFMain"

<UserControl.Resources>
    <c:GridData x:Key="dataforGrid"/>
</UserControl.Resources>
<Grid>
    <DataGrid Grid.Row="2" x:Name="datagridMain" ItemsSource="{Binding Source={StaticResource dataforGrid}, Path=Results, Mode=TwoWay}" />
</Grid>

Code Behind for UserControl above:

public GridControl()
    {
        InitializeComponent();

        GridData gd = new GridData();
        gd.UpdateResults();

        //datagridMain.ItemsSource = gd.Results;  
        -- This code above will work if I uncomment but I want it to be bound 
           directly and was curious as I thought the mode of 'two way' would 
           do this.  I am not certain and most examples assume property is already
           set up and not being created and updated.
    }

Code Class for GridData:

class PersonName
{
    public string Name { get; set; }
}

class GridData
{
    public ObservableCollection<PersonName> Results { get; set; }

    public void UpdateResults()
    {
        using (EntityDataModel be = new EntityDataModel())
        {
            var list = be.tePersons.Select(x => new PersonName { Name = x.FirstName });

            Results = new ObservableCollection<PersonName>(list);
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:26:03+00:00Added an answer on June 15, 2026 at 9:26 am

    To use binding like this, you need to:

    1. Set the DataContext correctly on the DataGrid (or on one of its parent)
    2. Implement INotifyPropertyChanged on your model class, and raise PropertyChanged in the property setter.

    1)

    Set your window’s DataContext to the GridData object:

    public GridControl()
    {
        InitializeComponent();
    
        GridData gd = new GridData();
        gd.UpdateResults();
        this.DataContext = gd;
    }
    

    2)

    Implement INotifyPropertyChanged. This ensures that your view gets notified when the Results property gets updated:

    public class GridData : INotifyPropertyChanged
    {
        private ObservableCollection<PersonName> _results;
        public ObservableCollection<PersonName> Results 
        {  
            get { return _results; }
            set
            {
                _results = value;
                RaisePropertyChanged("GridData");
            }
        }
    
        // ...
    
        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        private void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
            }
        }
        #endregion
    }
    

    Then you can simply bind to the path relative to the data context.

    <DataGrid ItemsSource="{Binding Results}" />
    

    Note that you don’t need two-way binding in this case — that’s for propagating changes from the View back to your model (ie, most useful for when there’s a UI control like a text box or checkbox).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to bind datagrid view column visibility with a property of class. I
i have 4 data sources that i Want to bind to a datagridview, so
I want to bind my ListView control to a generic list of objects? I
I want to bind the user score to a text box on the windows
Currently i have a datagrid view that displays names in the 1st columnnd in
I have a List that I want to bind to a DataGridView in a
I have a DataGrid and two StaticResource . I want to bind RowStyle of
I want to create Gridview similar to WPF datagrid i don't know how many
I want to bind a function to an event but I want to change
I want to bind a string value to a text box but only if

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.