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 9063581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T16:02:27+00:00 2026-06-16T16:02:27+00:00

I need to put some data in a DataGrid , using a custom UserControl

  • 0

I need to put some data in a DataGrid, using a custom UserControl for each row. Displaying the data works just fine, but when I edit the fields in the custom UserControl, the bound data records don’t change.

If I use a ListBox to display the data instead, it all works as expected. But I would rather use the DataGrid, which allows for sorting and (hopefully) adding new records.

To illustrate, here is a simple data class that I need to display (and edit) – persons and their spouses:

public class PersonViewModel : INotifyPropertyChanged
{
    public PersonViewModel(string name)
    {
        _name = name;
    }

    private string _name = null;
    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            this.PropertyChanged(this, new PropertyChangedEventArgs("Name"));
        }
    }

    private PersonViewModel _spouse = null;
    public PersonViewModel Spouse
    {
        get { return _spouse; }
        set
        {
            _spouse = value;
            this.PropertyChanged(this, new PropertyChangedEventArgs("Spouse"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged = (s, e) => { };
}

..and here is the custom UserControl (PersonView):

<UserControl x:Class="TestDataGrid.PersonView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" >
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <!--Editing in DataGrid works only if these bindings use UpdateSourceTrigger=PropertyChanged-->
        <TextBox Text="{Binding Path=Name}" Width="70" />
        <TextBlock Text="is married to" Margin="6,0" Grid.Column="1" />
        <TextBox Text="{Binding Path=Spouse.Name}" Width="70" Grid.Column="2" />
    </Grid>
</UserControl>

Finally, the main window (with code-behind) to put it all together:

<Window x:Class="TestDataGrid.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:TestDataGrid"
        Title="MainWindow" Height="350" Width="500">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>

        <!--Changes made here correctly update the persons' names,
            and the changes are reflected in the DataGrid below-->
        <ListBox x:Name="_listbox" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <local:PersonView />
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <!--Changes made here never reach the PersonViewModels..-->
        <DataGrid x:Name="_datagrid" Grid.Column="1" 
                  AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Name}" Header="Name" />
                <DataGridTextColumn Binding="{Binding Path=Spouse.Name}" Header="Spouse" />
            </DataGrid.Columns>
            <DataGrid.RowStyle>
                <Style TargetType="DataGridRow" >
                    <Setter Property="Template" >
                        <Setter.Value>
                            <ControlTemplate>
                                <local:PersonView />
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGrid.RowStyle>
        </DataGrid>
    </Grid>
</Window>

…

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var personA = new PersonViewModel("Alice");
        var personB = new PersonViewModel("Barry");
        var personC = new PersonViewModel("Carl");
        var personD = new PersonViewModel("Doris");

        personA.Spouse = personB;
        personC.Spouse = personD;
        var persons = new List<PersonViewModel>() { personA, personC };

        _listbox.ItemsSource = persons;
        _datagrid.ItemsSource = persons;
    }

}

What can I do to make editing work in the DataGrid, as it does in the ListBox?

  • 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-16T16:02:29+00:00Added an answer on June 16, 2026 at 4:02 pm

    I twiddled with your code a bit and I think I got it working.

    First of all, you have a note on your PersonView that says “Editing in DataGrid works only if these bindings use UpdateSourceTrigger=PropertyChanged.” I found that it WAS necessary to set UpdateSourceTrigger, but I used LostFocus to try and keep the same behavior as is in the ListBox.

    <!--Editing in DataGrid works only if these bindings use UpdateSourceTrigger-->
        <TextBox Text="{Binding Path=Name, UpdateSourceTrigger=LostFocus}" Width="70" />
        <TextBlock Text="is married to" Margin="6,0" Grid.Column="1" />
        <TextBox Text="{Binding Path=Spouse.Name, UpdateSourceTrigger=LostFocus}" Width="70" Grid.Column="2" />
    

    Second, instead of using 2 DataGridTextColumns and DataGrid.RowStyle, I used a single DataGridTemplateColumn. I hope this is acceptable–it appears the way you had it before, the template wasn’t really honoring the columns, but it does now.

            <DataGrid x:Name="_datagrid" Grid.Column="1" AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False" >
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="People">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <local:PersonView />
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    

    After making these changes, I saw it updating on both sides.

    I hope that helps you.

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

Sidebar

Related Questions

I have a Datagrid I want to provide some data validation, but I need
I have a table with some fixed columns id,title,created.., but there I need put
I need to analyse some data, stored in csv files. Each file represents the
I need to copy some data from one table to another but I need
I'm trying to put some data in db but something is going wrong. This
I have some data I need to put into a file, however I want
I'm using the Infragistics UltraWinGrid to present some data. I need some Excel copy/paste
I need to put some links in one TextView . For better user experience,
I need to put some of the entities created via a.dbml Linq-To-Sql file into
I have a design question. We need to put some default, system defined content

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.