DataGridTemplateColumn Two way binding is not working
Solution 1
I had the same problem, not updating the source:
<DataGridTemplateColumn Header="Obs" IsReadOnly="False">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBox Name="txtObs" Width="80"
Text="{Binding Path=ObsPedido, Mode=TwoWay}"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
For me it worked just adding UpdateSourceTrigger=PropertyChanged
<TextBox Name="txtObs" Width="80"
Text="{Binding Path=ObsPedido, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
Solution 2
I had the same problem and the solution posted by @jrivam did not help. For me to get my binding to work correctly I had to change the CellEditingTemplate
to use the OneWayToSource
Binding Mode.
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=IC_DEF_CHAR_TEXT,
Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}"/>
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>
Related videos on Youtube
David Potter
Updated on June 11, 2022Comments
-
David Potter almost 2 years
I've got a
DataGrid
I've bound to aSqlDataApter
. If I set up the XAML for the grid usingDataTextColumn
as illustrated in the code below it works perfectly<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="27,42,0,0" Name="dataGrid1" VerticalAlignment="Top" AreRowDetailsFrozen="True"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding KEY}" Visibility="Hidden" IsReadOnly="True"/> <DataGridTextColumn Binding="{Binding CHARACTERISTIC_CODE}" Header="Unit" IsReadOnly="True"/> <DataGridTextColumn Binding="{Binding UNIT_CHAR}" Header="Unit" IsReadOnly="True" /> <DataGridTextColumn Binding="{Binding IC_DEF_CHAR_NUMERIC}" Header="Number" IsReadOnly="False"/> <DataGridTextColumn Binding="{Binding IC_DEF_CHAR_TEXT}" Header="Text" IsReadOnly="False" /> <DataGridTextColumn Binding="{Binding IsNumeric}" Header="Status" IsReadOnly="True"/> <DataGridTextColumn Binding="{Binding IsText}" Header="Status" IsReadOnly="True"/> </DataGrid.Columns> </DataGrid>
I am binding this to a
DataTable
in code using:dataGrid1.ItemsSource = dTable.DefaultView
And I have a button that saves the changes using the ذSqlDataAdapterذ update method:
dAdapter.Update(dTable)
The problem is that I want to disable editing the
IC_DEF_CHAR_TEXT
field when the recordisNumeri
c and theIC_DEF_CHAR_TEXT
when the recordIsText
. I tried binding to theIsReadOnly
property but found that it is not bind-able, so I created templates for the two fields and bound theIsEnabled
property to theIsText
andIsNumeric
fields.<DataGrid AutoGenerateColumns="False" HorizontalAlignment="Left" Margin="27,42,0,0" Name="dataGrid1" VerticalAlignment="Top" AreRowDetailsFrozen="True"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding KEY}" Visibility="Hidden" IsReadOnly="True"/> <DataGridTextColumn Binding="{Binding CHARACTERISTIC_CODE}" Header="Unit" IsReadOnly="True"/> <DataGridTextColumn Binding="{Binding UNIT_CHAR}" Header="Unit" IsReadOnly="True"/> <DataGridTemplateColumn Header="Numeric"> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding Path=IC_DEF_CHAR_NUMERIC, Mode=TwoWay}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <TextBox IsReadOnly="False" Text="{Binding Path=IC_DEF_CHAR_NUMERIC, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn Header="Text" > <DataGridTemplateColumn.CellTemplate> <DataTemplate> <TextBox Text="{Binding Path=IC_DEF_CHAR_TEXT, Mode=TwoWay}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> <DataGridTemplateColumn.CellEditingTemplate> <DataTemplate> <TextBox Text="{Binding Path=IC_DEF_CHAR_TEXT, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> </DataTemplate> </DataGridTemplateColumn.CellEditingTemplate> </DataGridTemplateColumn> </DataGrid.Columns> </DataGrid>
This worked exactly like I wanted, the textboxes were enabled when necessary. However the changes made in the
TextBoxes
are no longer saved to the database during update. Can someone out there explain to me why the database is no longer being updated?-
jamesSampica almost 11 yearsTry changing the
CellTemplate
to not use two-way binding as it's essentially only supposed to act as a window. -
David Potter over 10 yearsI originally set it up that way and that didn't work either. I think two way is the default but I'm not 100% sure of that.
-
jamesSampica over 10 yearsWhat I mean is that
TextBox
will default toTwo-Way
, try changing it toOne-Way
on theCellTemplate
. You only wantTwo-Way
onCellEditingTemplate
-
David Potter over 10 yearsI just tried that and that didn't help either. Thanks for trying
-
jamesSampica over 10 yearsWhat do you mean when you say I tried binding to the IsReadOnly property but found that it is not bindable? You should be able to bind to this. What happened when you did?
-
David Potter over 10 yearsBasically it did not do anything if I tried to bind it to a boolean field. I could set it true or false but when I tried to bind it it was always editable. I would like to find out why the twoway binding is not working on the textbox as I can see me having to use templates in my coding quite often.
-
kub1x over 5 yearsI didn't even have to read the question content, just the topic, and the answer was exactly what I was looking for. Guess that really is just a very stupid default behavior of
DataGrid
.
-
-
Teslo. about 9 yearsI had the same problem and setting the
UpdateSourceTrigger=PropertyChanged
solved it. I guess this should be marked as answer. -
AnjumSKhan over 8 yearsThis solved my problem too. This is a bug with only DataGrid, I think. 2 way binding works as expected with normal fields outside DataGrid.
-
Zerratar over 8 yearsHad the same issue and this solved it for me as well! :-) Thank you.
-
LaoR over 8 yearsOh my God! I was searching for this solution about 2 hours! I've even started to looking on implementing "TrueObservableCollection", but I've finally found this! Thanks!
-
Chris Staley over 7 years
UpdateSourceTrigger=LostFocus
also works, in case you don't want to update on every key press. -
tequilacat about 6 yearsI wish it could help me but I project text input into double variable, replacing invalid input with default value. So as soon as user edits text to a not-a-number (like placing decimal point after a digit or erasing the field) it's replaced with default value. So I still have to find solution not involving UpdateSourceTrigger=PropertyChanged