2010-07-02 71 views
0

我有一個WPF數據網格的用戶控件。我使用元素主機在Windows窗體中使用此用戶控件。Windows窗體中WPF用戶控件的雙向DataBinding

我可以使用的ListCollectionView將數據綁定到用戶控件但是當我進行更新到DataGrid的變化不被反射回來。

我設置模式=雙向,但沒有用。

任何想法?

這裏是我的代碼示例:

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

在我的Windows窗體的frm1的代碼是:

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

我必須將數據保存回我的數據庫。我正在使用數據集和數據表。

請幫我一下嗎?


我試圖調試我的程序,我在輸出窗口出現錯誤。

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

這裏是我的資源窗口中的XAML代碼在我的用戶控制:

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

這是我在我的WPF的DataGrid代碼:

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

我不明白這錯誤。

+1

花一點時間格式化您的問題以便讀取,併爲需要幫助的N個讀者節省時間。每個人都看重他們自己的時間=>不想花時間試圖破譯格式不正確的問題。最大限度地獲得答案的機會... – Gishu 2010-07-02 05:17:44

回答

1

請問您的數據源執行INotifyCollectionChanged?否則WPF不知道有什麼不同。

0

可能是由於多種原因...沒有足夠的信息在這裏。

  1. 當您使用附加的調試器運行應用程序時,是否在輸出窗口中看到任何數據綁定錯誤?
  2. 不應該使用ObservableCollection而不是ListCollectionView作爲後備數據源嗎? Link我看不到實現INotifyPropertyChanged的ListCollectionView。
0

嘗試將「UpdateSourceTrigger=LostFocus」添加到LabelTextBoxColumn的綁定中。

此外,儘量不要從代碼隱藏填充DataSet。將我的數據集綁定到列表框時遇到了類似的問題。我會更改Listbox中的值,並且更改永遠不會從源代碼反饋回來。我發現我一直在用舊值填充數據集。

什麼我也忘了做的是明確的更新數據庫。我在你的代碼背後,進行更改後,請撥打DatasetTableAdapter.Update()方法。我不需要執行INotifyPropertyChanged