wpf DataGridTemplateColumn 两种方式绑定不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18216745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
DataGridTemplateColumn Two way binding is not working
提问by David Potter
I've got a datagrid I've bound to a SqlDataApter. If I set up the XAML for the grid using DataTextColumn as illustrated in the code below it works perfectly
我有一个绑定到 SqlDataApter 的数据网格。如果我使用 DataTextColumn 为网格设置 XAML,如下面的代码所示,它可以完美运行
<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>
<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>
I am binding this to a datatable in code using dataGrid1.ItemsSource = dTable.DefaultView and have a button that saves the changes using the SqlDataAdapter update method dAdapter.Update(dTable)
我使用 dataGrid1.ItemsSource = dTable.DefaultView 将此绑定到代码中的数据表,并有一个按钮使用 SqlDataAdapter 更新方法 dAdapter.Update(dTable) 保存更改
The problem is that I want to disable editing the IC_DEF_CHAR_TEXT field when the record isNumeric and the IC_DEF_CHAR_TEXT when the record IsText. I tried binding to the IsReadOnly property but found that it is not bindable, so I created templates for the two fields and bound the IsEnabled property to the IsText and IsNumeric fields.
问题是我想在记录为Numeric 时禁用编辑IC_DEF_CHAR_TEXT 字段,当记录为IsText 时禁用编辑IC_DEF_CHAR_TEXT 字段。我尝试绑定到 IsReadOnly 属性,但发现它不可绑定,因此我为这两个字段创建了模板并将 IsEnabled 属性绑定到 IsText 和 IsNumeric 字段。
<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>
<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>
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?
这与我想要的完全一样,必要时启用了文本框。但是,在更新期间不再将在 TextBox 中所做的更改保存到数据库中。有人可以向我解释为什么数据库不再更新吗?
回答by jrivam
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
对我来说,它只是添加 UpdateSourceTrigger=PropertyChanged
<TextBox Name="txtObs" Width="80" Text="{Binding Path=ObsPedido, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
回答by Chris J
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.
我遇到了同样的问题,@jrivam 发布的解决方案没有帮助。为了让我的绑定正常工作,我必须更改 CellEditingTemplate 以使用 OneWayToSource 绑定模式。
<DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBox Text="{Binding Path=IC_DEF_CHAR_TEXT, Mode=OneWayToSource, UpdateSourceTrigger=PropertyChanged}" />
</DataTemplate>
</DataGridTemplateColumn.CellEditingTemplate>

