antoine
antoine

Reputation: 21

How to add binding to a column in a datagrid?

I'm having troubles with binding in my project. I have a datagrid in wich I define 2 columns by default. The ItemsSource binds to a list (ListeGrilleTarifaire) and the content of columns binds to an element of a second list (ListeTranche) contained into the first one.

<techuc:DataGridM Name="dg_HeaderTranche" AutoGenerateColumns="False" ItemsSource="{Binding ListeGrilleTarifaire}" CanUserAddRows="False" CanUserDeleteRows="False" Visibility="{Binding accesGrilleMultiDepartement}" Height="42"  Margin="440,0,0,0" VerticalAlignment="Top">
<DataGrid.Columns>                                                   
    <DataGridTextColumn Header="{x:Static trad:Langues.lblTranche1}" Binding="{Binding ListeTranche[0],Mode=TwoWay}" Width="80"/>
    <DataGridTextColumn Header="{x:Static trad:Langues.lblTranche2}" Binding="{Binding ListeTranche[1],Mode=TwoWay}" Width="80"/>
</DataGrid.Columns>

I Would like have the possibility to add column with a button and to bind the new column with the second list.

I my code behind, when I click on the button, I add an element to my the second list and the followinf code is executed

DataGridTextColumn textColumnTranche = new DataGridTextColumn();
textColumnTranche.Header = "Tranche X";
textColumnTranche.Binding = new Binding("ListeTranche[3]");
dg_HeaderTranche.Columns.Add(textColumnTranche);

My new column is created and I can fill it but when I save data in the database, values are still at 0. I thonk it's because I don't configure the binding mode as TwoWay but I don't know how to do that in code behind.

My lists are defined and used in the ViewModel.

Upvotes: 2

Views: 531

Answers (1)

Florian Gl
Florian Gl

Reputation: 6014

How to set BindingMode = TwoWay:

textColumnTranche.Binding = new Binding("ListeTranche[3]") { Mode = BindingMode.TwoWay };

Upvotes: 1

Related Questions