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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:37:14+00:00 2026-05-30T21:37:14+00:00

I have a DataGrid with two way binding and not sure why this does

  • 0

I have a DataGrid with two way binding and not sure why this does not work, any help would be appreciated.

I wanted to dynamically bind to the DataGrid using a twoway binding object.

I used the columns in XAML. If I just set the ‘ItemSource” property directly – it works but then the two binding doesn’t work – if I change my source in code the Grid doesn’t reflect that change.

I created a simple sample to illustrate my setup

Here is the XAML

<UserControl x:Class="SilverlightApplication1.MainPage"
    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"
    mc:Ignorable="d"
    d:DesignHeight="356" d:DesignWidth="590" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

    <Grid x:Name="LayoutRoot" Background="White"> 
        <sdk:DataGrid AutoGenerateColumns="False" Height="136" HorizontalAlignment="Left" Margin="71,116,0,0" Name="MyGrid" VerticalAlignment="Top" Width="453" >
            <sdk:DataGrid.Columns>
                <sdk:DataGridTextColumn Binding="{Binding Path=Label, Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Width="Auto" Header="Selected" />
                <sdk:DataGridTextColumn Binding="{Binding Path=YValue, Mode=TwoWay}" CanUserReorder="True" CanUserResize="True" CanUserSort="True" Header="Name" Width="*" />
            </sdk:DataGrid.Columns>
        </sdk:DataGrid>
    </Grid>
</UserControl>

Here is the code behind

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent(); 
            ObservableCollection<Value> values = new ObservableCollection<Value>(); 
            values.Add(new Value() { Label = "Sony", YValue = 50 });
            values.Add(new Value() { Label = "Dell", YValue = 35 });
            values.Add(new Value() { Label = "HP", YValue = 27 });
            values.Add(new Value() { Label = "HCL", YValue = 17 });
            values.Add(new Value() { Label = "Toshiba", YValue = 16 });

            PagedCollectionView p = new PagedCollectionView(values); 

            Binding b = new Binding("ValuesBinding");
            b.Mode = BindingMode.TwoWay;
            b.Source = values;
            MyGrid.SetBinding(DataGrid.ItemsSourceProperty, b);  
        }
    }

    public class Value : INotifyPropertyChanged
    {
        public String Label
        {
            get
            { return _label; }
            set
            {
                _label = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("Label"));
            }
        }
         public Double YValue
        {
            get
            {return _yValue;}
            set
            {
                _yValue = value;
                if (PropertyChanged != null)
                    PropertyChanged(this, new PropertyChangedEventArgs("YValue"));
            }
        }
        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
        Double _yValue;
        String _label;
    } 

}
  • 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-05-30T21:37:16+00:00Added an answer on May 30, 2026 at 9:37 pm

    There are a couple of problems that I can see here.

    The first line that you use to create your binding is

    Binding b = new Binding("ValuesBinding");
    

    This won’t do what you want. The string ValuesBinding is being used as a property-path, and the ObservableCollection you’re binding the DataGrid to has no property on it named ValuesBinding. Indeed, if you look in the VS Output window, you should see a message such as

    System.Windows.Data Error: BindingExpression path error: 'ValuesBinding' property not found on 'System.Collections.ObjectModel.ObservableCollection`1 ...
    

    However, if you remove "ValuesBinding" from the above to leave you with

    Binding b = new Binding();
    

    then you get an error about two-way bindings needing a Path. However, you don’t need a two-way binding here. You can simply remove the line b.Mode = BindingMode.TwoWay; and the error goes away.

    Two-way bindings are used to allow the view-layer to set properties in the view-model layer. The Path specifies where to find the view-model property to set. However, since you’re binding straight to a collection, there’s no property involved and hence nothing that the view-layer could set.

    In your case, this binding doesn’t need to be two-way. Changes to the collection itself (e.g. adding or removing items) can still be made, even when using a one-way binding for the ItemsSource. The two-way bindings you have on the Label and YValue properties of your Value class will also work as you expect. Setting a one-way binding on the DataGrid’s ItemsSource doesn’t make the whole grid read-only.

    Finally, I’m not sure why you’re creating a binding in code-behind to bind to a collection already available in the code-behind. You can achieve the same by just writing

    MyGrid.ItemsSource = values;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a scenario that does not seem to be covered in any of
Can i add two header text in Datagrid? My Requirement is to have two
I have a DataGrid where each column has a SortExpression. I would like the
I have a dataGrid(not dataGridView) in which i need to add Checkbox dynamically at
I have a SL DataGrid that has two columns. I need to be able
I have two datagrids with one column each. First: <DataGrid.Columns> <DataGridTextColumn x:Name=FilterTextCol01 IsReadOnly=False Width={Binding
I have a DataGrid that is used in two different views. In each case,
I have a form that consists of two data grids and a button. Datagrid
We have an app that uses simple one way binding with a GridView to
I have a datagrid that displays items with two columns of text row data

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.