我有一個WPF數據網格綁定到一個EF6 Dbcontext。我有許多功能,例如手動編輯等。WPF EF6 Datagrid單元不會更新編程方式更改,除非用戶編輯單元格
問題 編輯綁定到網格的EF對象不會在網格上更新。但如果我手動編輯一個單元格後,這些背景編輯之一,它立即顯示正確的值。
SCENARIO/SETUP
ViewSources涉及
<Window.Resources>
<CollectionViewSource x:Key="equipmentViewSource" d:DesignSource="{d:DesignInstance {x:Type HAI_Job_EF_Model:Equipment}, CreateList=True}"/>
<CollectionViewSource x:Key="equipmentAssociatedDevicesViewSource"
Source="{Binding AssociatedDevices, Source={StaticResource equipmentViewSource}}"/>
</Window.Resources>
注意,相關聯的設備是一個設備對象內部一個ObservableCollection。
WPF Datagrid的(僅作爲示例份)
<DataGrid x:Name="associatedDevicesDataGrid" Grid.Row="1" AutoGenerateColumns="False"
MaxWidth="1200" EnableRowVirtualization="True"
RowDetailsVisibilityMode="VisibleWhenSelected" CanUserDeleteRows="False"
DataContext="{StaticResource equipmentAssociatedDevicesViewSource}" ItemsSource="{Binding}"
CellEditEnding="associatedDevicesDataGrid_CellEditEnding"
SelectionChanged="associatedDevicesDataGrid_SelectionChanged"
PreviewKeyDown="associatedDevicesDataGrid_PreviewKeyDown"
LostFocus="associatedDevicesDataGrid_LostFocus">
<DataGrid.Resources>
<!-- DATGRID STYLE CELL: Gives padding space inside cells -->
<Style TargetType="DataGridCell">
<Setter Property="Padding" Value="5,5"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type DataGridCell}">
<Border Padding="{TemplateBinding Padding}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
<ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn x:Name="quantityColumn" Header="Qty" MaxWidth="50"
Binding="{Binding Quantity, StringFormat={}\{0:N0\}, Mode=TwoWay, UpdateSourceTrigger=LostFocus}"/>
<DataGridComboBoxColumn x:Name="typeColumn" Header="Type" MaxWidth="150"
ItemsSource="{Binding Source={StaticResource assDevTypeFilteredViewSource}}"
SelectedItemBinding="{Binding Path=Type, UpdateSourceTrigger=PropertyChanged}"
TextBinding="{Binding Path=Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<DataGridComboBoxColumn.EditingElementStyle>
<Style TargetType="ComboBox">
<Setter Property="IsEditable" Value="True"/>
<Setter Property="Text" Value="{Binding Path=Type}"/>
<Setter Property="IsSynchronizedWithCurrentItem" Value="True" />
<Setter Property="IsTextSearchEnabled" Value="True" />
<Setter Property="IsTextSearchCaseSensitive" Value="False" />
</Style>
</DataGridComboBoxColumn.EditingElementStyle>
</DataGridComboBoxColumn>
<DataGridTemplateColumn x:Name="certificateNumberColumn" Header="Certificate Number" Width="Auto" MaxWidth="200">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox Name="cbxAssDevCertComboBox"
IsEditable="True" IsTextSearchEnabled="True" IsTextSearchCaseSensitive="False"
ItemsSource="{Binding GenericFilterResults}"
Text="{Binding Path=CertificateNumber, UpdateSourceTrigger=PropertyChanged}"
SelectionChanged="cbxAssDevCertComboBox_SelectionChanged"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn x:Name="manufacturerColumn" Header="Manufacturer" Width="Auto" MaxWidth="150"
Binding="{Binding Manufacturer, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
背景變化 組合框 「cbxAssDevCertComboBox」 SelectionChanged事件然後更新該其他細胞源對象中的所有值。這是不刷新的更新。
主要選擇受審/考慮的
我不完全了MVVM,我不跟隨它。我現在太深刻了,無法改變這個項目。我只需要f'n的東西來更新......!
我無法在Observable集合中包裝每一個EF對象......沒有其他東西需要它,那麼爲什麼這樣呢?那不是CollectionViewSource本質上在做什麼?!
更新Datagrids ItemSource bindingexpression沒有幫助。
使用更新
如何更新細胞就像它當我嘗試手動編輯效果的PROGRAMITC編輯後?
我現在已經失去了幾天試圖解決這個問題;任何幫助將不勝感激。
我想你需要在模型上實現'INotifyPropertyChanged',並在值被改變時調用它。 ObservableCollection'會在集合被修改(添加新項目/刪除項目)時通知用戶界面,但當項目的屬性發生變化時,通知用戶界面 - 因爲您需要'INotifyPropertyChanged'。 – Shaamaan
很高興知道可觀察收集和我通知的區別。我認爲使用集合viewsource也一樣嗎?顯然不是我想的。手動觸發綁定刷新怎麼辦? – Asvaldr
你打算如何觸發這種綁定更新? – Shaamaan