DataGridTemplateColumn Two way binding is not working

好久不见. 提交于 2020-01-30 20:15:12

问题


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>

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)

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>

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?


回答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}"/>



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


来源:https://stackoverflow.com/questions/18216745/datagridtemplatecolumn-two-way-binding-is-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!