tags:

views:

38

answers:

4

Hi,

I have a user control with a WPF Data Grid. I used this user control in a Windows form using Element Host.

I can bind the data to the user control using ListCollectionView but when i make updates to the datagrid the changes are not reflected back.

I set the Mode = TwoWay but no use.

Any ideas ?

Here is a sample of my code:

UserControl.xaml

<my:DataGrid ItemsSource="{Binding}"
             HorizontalScrollBarVisibility="Hidden" SelectionMode="Extended"
             CanUserAddRows="False" CanUserDeleteRows="False"
             CanUserResizeRows="False" CanUserSortColumns="False"
             AutoGenerateColumns="False"
             RowHeaderWidth="20" RowHeight="25" Width="Auto" Height="Auto"
             RowStyle="{StaticResource RowSelected1}"
             CellStyle="{StaticResource RowSelected}"
             GridLinesVisibility="Horizontal" >
  <my:DataGrid.GroupStyle>
    <GroupStyle ContainerStyle="{StaticResource GroupHeaderStyle1}">
      <GroupStyle.Panel>
        <ItemsPanelTemplate>
          <my:DataGridRowsPresenter/>
        </ItemsPanelTemplate>
      </GroupStyle.Panel>
    </GroupStyle>
  </my:DataGrid.GroupStyle>
  <my:DataGrid.Columns>
    <Controls:LabelTextBoxColumn   Header="Tread BarCode" Width="2*"   
    HorizontalAlignment="Left" VerticalAlignment="Center"
    ElementStyle="{StaticResource BaseLabelCellStyle}"
    EditingElementStyle="{StaticResource BaseTextBoxCellStyle}"
    Binding="{Binding Name,Mode=TwoWay}"/>

The code in my Windows form frm1 is :

this.sdaTrueName.Fill(this.dstrueSrch1.dtTrue);
view = new ListCollectionView(this.dstrueSrch1.dtTrue.ToList());
view.GroupDescriptions.Add(new PropertyGroupDescription("Name"));
UserControlABC.DataContext = view;

I have to save the data back into my database.I am using Dataset and DataTables.

Please help me with any ideas ?

+1  A: 

Does your Data source implement INotifyCollectionChanged? Otherwise WPF doesn't know that something is different.

Paul Betts
A: 

Could be due to multiple reasons... not enough info here.

  1. Do you see any data binding errors in the output window, when you're running the app with debugger attached ?
  2. Shouldn't you be using ObservableCollection instead of ListCollectionView for the backing data source ? Link I don't see ListCollectionView implementing INotifyPropertyChanged.
Gishu
A: 

Try adding "UpdateSourceTrigger=LostFocus" to the Binding of the LabelTextBoxColumn.

Also, try not to fill the Dataset from the Code-Behind. I had a similar issue when binding my Dataset to a Listbox. I would change the value in the Listbox and the change would never reflect back from the source. I found that i kept on filling the Dataset with old values.

And what i also forgot to do is to update the Database Explicitly. I your Code-Behind, after making changes, call the .Update() Method.

I did not need to implement INotifyPropertyChanged.

GaaTY
A: 

Hi,

Everybody. thanks for your answers. I tried to debug my program and i was getting an error in the output window.

*"System.Windows.Data Warning: 52 : Created BindingExpression (hash=19378226) for Binding (hash=19699911)

System.Windows.Data Warning: 54 : Path: 'IsEnabled'

System.Windows.Data Warning: 56 : BindingExpression (hash=19378226): Default mode resolved to OneWay

System.Windows.Data Warning: 57 : BindingExpression (hash=19378226): Default update trigger resolved to PropertyChanged

System.Windows.Data Warning: 58 : BindingExpression (hash=19378226): Attach to System.Windows.Controls.TextBox.IsEnabled (hash=56309765)

System.Windows.Data Warning: 63 : BindingExpression (hash=19378226): Resolving source

System.Windows.Data Warning: 66 : BindingExpression (hash=19378226): Found data context element: TextBox (hash=56309765) (OK)

System.Windows.Data Warning: 67 : BindingExpression (hash=19378226): DataContext is null

System.Windows.Data Warning: 61 : BindingExpression (hash=19378226): Resolve source deferred

System.Windows.Data Warning: 63 : BindingExpression (hash=19378226): Resolving source

System.Windows.Data Warning: 66 : BindingExpression (hash=19378226): Found data context element: TextBox (hash=56309765) (OK)

System.Windows.Data Warning: 74 : BindingExpression (hash=19378226): Activate with root item dataTableExtrRow (hash=24854661)

System.Windows.Data Warning: 104 : BindingExpression (hash=19378226): At level 0 - for dataTableExtrRow.IsEnabled found accessor

System.Windows.Data Error: 39 : BindingExpression path error: 'IsEnabled' property not found on 'object' ''dataTableExtrRow' (HashCode=24854661)'.

BindingExpression:Path=IsEnabled; DataItem='dtextruderWindupRow' (HashCode=24854661); target element is 'TextBox' (Name=''); target property is 'IsEnabled' (type 'Boolean')

System.Windows.Data Warning: 76 : BindingExpression (hash=19378226): TransferValue - got raw value {DependencyProperty.UnsetValue}

System.Windows.Data Warning: 84 : BindingExpression (hash=19378226): TransferValue - using fallback/default value 'True'

System.Windows.Data Warning: 85 : BindingExpression (hash=19378226): TransferValue - using final value 'True'"*

Here is the XAML code in my Resources window in my User Control:

<Style x:Key="BaseTextBoxCellStyle" TargetType="{x:Type TextBox}">
       <Setter Property="IsEnabled" Value="{Binding   
              IsEnabled,diag:PresentationTraceSources.TraceLevel=High}"/>
 </Style>
  <Style x:Key="BaseLabelCellStyle" TargetType="{x:Type Label}">
         <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
         <Setter Property="Margin" Value="0,-3,0,0"/>
 </Style>

Here is my Code in my WPF DataGrid:

<Controls:LabelTextBoxColumn
      Header="Tread BarCode" Width="2*"
          HorizontalAlignment="Left" VerticalAlignment="Center"
          ElementStyle="{StaticResource BaseLabelCellStyle}"
          EditingElementStyle="{StaticResource BaseTextBoxCellStyle}"
      Binding="{Binding rollCallID,Mode=TwoWay}"/>

I can't understand this error.

NewDeveloper
The only actual error in that long bolus of text is the one telling you that `IsEnabled` isn't a property of the `dataTableExtrRow` object, which nothing you've posted here addresses.
Robert Rossney
@Robert RossneyI don't understand what you say.
NewDeveloper
This: `System.Windows.Data Error: 39 : BindingExpression path error: 'IsEnabled' property not found on 'object' ''dataTableExtrRow' (HashCode=24854661)'.` One of your styles is trying to bind to the `IsEnabled` property on an object that doesn't have an `IsEnabled` property.
Robert Rossney
@RobertRossney, Thanks for the tip. But my dataSource still does not bind to the database. I get this error now. System.Windows.Data Error: 39 : BindingExpression path error: '' property not found on 'object' ''BindingListCollectionView' (HashCode=28574894)'. BindingExpression:Path=; DataItem='BindingListCollectionView' (HashCode=28574894); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
NewDeveloper
That's telling you that you have a Binding somewhere whose Path property isn't being set.
Robert Rossney
@RobertRossneyExcept for the WPF DataGrid where i set the binding as <my:DataGrid ItemsSource="{Binding}" />The rest of all my columns in the WPF Datagrid have a Path Property set.
NewDeveloper