DataGridTemplateColumn双向绑定不起作用 [英] DataGridTemplateColumn Two way binding is not working

查看:100
本文介绍了DataGridTemplateColumn双向绑定不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到SqlDataApter的数据网格。如果我使用DataTextColumn为网格设置XAML,如下面的代码所示,则效果很好

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

<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>

我将此绑定到使用
的代码中的datatable dataGrid1.ItemsSource = dTable.DefaultView
,并具有一个按钮,该按钮使用SqlDataAdapter更新方法
来保存更改dAdapter.Update(dTable)

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)

问题是我想在记录为Numeric时禁用编辑IC_DEF_CHAR_TEXT字段,而在记录IsText时禁用IC_DEF_CHAR_TEXT字段。我尝试绑定到IsReadOnly属性,但发现它不可绑定,因此我为两个字段创建了模板,并将IsEnabled属性绑定到IsText和IsNumeric字段。

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.

<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>

,则在必要时启用了文本框。但是,在更新过程中,在TextBoxes中所做的更改不再保存到数据库中。有人可以向我解释为什么不再更新数据库吗?

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?

推荐答案

我遇到了同样的问题,没有更新来源:

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>

对我来说,它只是添加 UpdateSourceTrigger = PropertyChanged

For me it worked just adding UpdateSourceTrigger=PropertyChanged

<TextBox Name="txtObs" Width="80"  Text="{Binding Path=ObsPedido, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

这篇关于DataGridTemplateColumn双向绑定不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆