Skip to main content

Microsoft Silverlight

Answered Question Datagrid and DatabindingRSS Feed

(0)

R.Vijay
R.Vijay

Member

Member

0 points

3 Posts

Datagrid and Databinding

Hi,

     i've a class named Employee which has two properties Name and Age.I've created a list of employees and set as the ItemsSource and added the below columns to the datagrid.<data:DataGridTextColumn Header="Name" Binding="{Binding Name}"/>

<data:DataGridTextColumn Header="Age" Binding="{Binding Age}"/>

But when i try to edit the cell ,the source gets the latest values which is the oppsoite behaviour of oneway mode.Guess,OneWay(from source to target ) is the default mode in databinding .Does silverlight has OneWayToSource Binding Mode ?

 

Ardman
Ardman

Contributor

Contributor

3434 points

946 Posts

Re: Datagrid and Databinding

The Mode on the Binding is set to OneWay indicating that the Name will come from the Binding Source (your Employee object) to be bound to the DataGridTextColumn, but changes made in the UI will not be sent back to the Binding Source.

R.Vijay
R.Vijay

Member

Member

0 points

3 Posts

Re: Datagrid and Databinding

yeah,i too assumed it in the same way as you mentioned .But the datagrid behaves in a peculiar way .I think ,by default it uses the two way mode .

I tried the same  scenario with textbox and it works perfect.Not sure where it goes wrong ,when i try it with the datagrid.

MComeauS2S
MComeauS2S

Member

Member

284 points

155 Posts

Re: Datagrid and Databinding

You need to add Mode=TwoWay on your binding because DataGrid column bindings are OneWay by default.

yifung
yifung

Contributor

Contributor

3315 points

541 Posts

Microsoft
Answered Question

Re: Datagrid and Databinding

Yes, the DataGrid by default changes Bindings to be TwoWay.  If you don't want to edit you can set IsReadOnly to true for particular columns or the entire DataGrid

Yifung Lin [MSFT]

R.Vijay
R.Vijay

Member

Member

0 points

3 Posts

Re: Datagrid and Databinding

Thanks yifung.

Is this the expected behaviour in Datagrid or is it a bug ?

Ardman
Ardman

Contributor

Contributor

3434 points

946 Posts

Re: Re: Datagrid and Databinding

Looks like its expected behaviour:

http://msdn.microsoft.com/en-us/library/system.windows.controls.datagrid(VS.95).aspx

yifung
yifung

Contributor

Contributor

3315 points

541 Posts

Microsoft
Answered Question

Re: Re: Datagrid and Databinding

Yes, the behavior is by design.

Yifung Lin [MSFT]
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities