开发者

Two Way DataBinding with WPF User Control in Windows Forms

开发者 https://www.devze.com 2023-01-05 16:49 出处:网络
I have a user control with a WPF Data Grid. I used this user control in a Windows form using Element Host.

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"
开发者_C百科             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 ?


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


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


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.


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 DatasetTableAdapter.Update() method.

I did not need to implement INotifyPropertyChanged.

0

精彩评论

暂无评论...
验证码 换一张
取 消