我们维护一个WPF应用程序/控件。其中一个控件是存储数字的网格,用户可以编辑单元格 在WPF 3.0中,它工作正常 现在我们转移到WPF 4.5,相同的代码表现为不同:编辑单元格后,点击ENTER,它会导致"额外刷新"的网格。 我调试了,发现了这个:
public List<List<double>> DoubleArray { get; set; }
private void dataGrid2D_CellEditEnding(object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e)
{
...
DoubleArray[y][x] = double.Parse(textBox.Text);
GridContent = DoubleArray; <======= [this line]
...
}
public static readonly DependencyProperty GridContentProperty =
DependencyProperty.Register("GridContent", typeof(IList), typeof(GridEditor),
new UIPropertyMetadata(null,GridContentPropertyChanged));
public IList GridContent
{
get { return (IList)GetValue(GridContentProperty); }
set { SetValue(GridContentProperty, value); }
}
private static void GridContentPropertyChanged(DependencyObject source, DependencyPropertyChangedEventArgs e)
{
GridEditor editor = source as GridEditor;
editor.OnGridContentChanged(e.OldValue,e.NewValue);
}
public virtual void OnGridContentChanged(object oldValue, object newValue)
{
....
RebindData(dataGrid);
}
private void RebindData(DataGrid2DLibrary.DataGrid2DT grid)
{
Binding datagrid2dBinding = new Binding();
.....
datagrid2dBinding.Path = new PropertyPath("DoubleArray");
grid.SetBinding(DataGrid2DT.ItemsSource2DProperty, datagrid2dBinding);
}
标记的行GridContent = DoubleArray;
在WPF3和WPF4.5中运行不同
在WPF4.5中,它会导致GridContentPropertyChanged
被触发,而在WPF3中却没有。我想这应该是&#34;额外刷新&#34;。的原因
Callstack for this&#34; extra&#34;拨打:
OurEditor.dll!OurEditor.GridEditor.GridEditor.OnGridContentChanged(object oldValue, object newValue) Line 345 C#
OurEditor.dll!OurEditor.GridEditor.GridEditor.GridContentPropertyChanged(System.Windows.DependencyObject source, System.Windows.DependencyPropertyChangedEventArgs e) Line 120 C#
WindowsBase.dll!System.Windows.DependencyObject.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e) Unknown
PresentationFramework.dll!System.Windows.FrameworkElement.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs e) Unknown
WindowsBase.dll!System.Windows.DependencyObject.NotifyPropertyChange(System.Windows.DependencyPropertyChangedEventArgs args) Unknown
WindowsBase.dll!System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.EntryIndex entryIndex, System.Windows.DependencyProperty dp, System.Windows.PropertyMetadata metadata, System.Windows.EffectiveValueEntry oldEntry, ref System.Windows.EffectiveValueEntry newEntry, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType) Unknown
WindowsBase.dll!System.Windows.DependencyObject.InvalidateProperty(System.Windows.DependencyProperty dp, bool preserveCurrentValue) Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.Invalidate(bool isASubPropertyChange) Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpression.TransferValue(object newValue, bool isASubPropertyChange) Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpression.ScheduleTransfer(bool isASubPropertyChange) Unknown
PresentationFramework.dll!MS.Internal.Data.ClrBindingWorker.NewValueAvailable(bool dependencySourcesChanged, bool initialValue, bool isASubPropertyChange) Unknown
PresentationFramework.dll!MS.Internal.Data.PropertyPathWorker.UpdateSourceValueState(int k, System.ComponentModel.ICollectionView collectionView, object newValue, bool isASubPropertyChange) Unknown
PresentationFramework.dll!MS.Internal.Data.PropertyPathWorker.RefreshValue() Unknown
PresentationFramework.dll!MS.Internal.Data.ClrBindingWorker.RefreshValue() Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpression.UpdateTarget() Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.EndSourceUpdate() Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpression.UpdateSource(object value) Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.UpdateValue() Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpression.UpdateOverride() Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.Update() Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.ProcessDirty() Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.Dirty() Unknown
PresentationFramework.dll!System.Windows.Data.BindingExpressionBase.SetValue(System.Windows.DependencyObject d, System.Windows.DependencyProperty dp, object value) Unknown
WindowsBase.dll!System.Windows.DependencyObject.SetValueCommon(System.Windows.DependencyProperty dp, object value, System.Windows.PropertyMetadata metadata, bool coerceWithDeferredReference, bool coerceWithCurrentValue, System.Windows.OperationType operationType, bool isInternal) Unknown
WindowsBase.dll!System.Windows.DependencyObject.SetValue(System.Windows.DependencyProperty dp, object value) Unknown
OurEditor.dll!OurEditor.GridEditor.GridEditor.GridContent.set(System.Collections.IList value) Line 114 C#
OurEditor.dll!OurEditor.GridEditor.GridEditor.dataGrid2D_CellEditEnding(object sender, Microsoft.Windows.Controls.DataGridCellEditEndingEventArgs e) Line 674 C#
XAML如何使用此编辑器:
<ControlTemplate x:Key="GridEditorTemplate">
<ge:GridEditor IsReadOnly="{Binding IsOutput, RelativeSource={RelativeSource AncestorType={x:Type vo:OutputView}}}">
<me:GridEditor.GridContent>
<Binding Path="TheGridValue" Mode="TwoWay" Converter="{StaticResource gridConverter}">
</Binding>
</me:GridEditor.GridContent>
</me:GridEditor>
</ControlTemplate>
我对WPF并不熟悉,所以任何帮助/提示都可能有用,可以检查/如何使用相同的&#34;正确的&#34;我们对WPF3的行为。
如果需要任何其他信息,我可以尝试提供。
答案 0 :(得分:0)
在WPF4.5中,它会导致
GridContentPropertyChanged
被触发,而在WPF3中却没有。{/ p>
嗯,只要依赖属性设置为新值,就应该触发GridContentPropertyChanged
。这是预期的行为,显然这已在较新版本的.NET Framework中得到修复。因此4.5行为是预期的行为。
你可能会得到很多修复&#34;免费&#34;升级.NET版本时。如果这导致您的应用程序出现问题,那么您的代码可能最初以错误的方式编写。因此,您可以按照@Peter:
的建议避免升级或修复代码public virtual void OnGridContentChanged(object oldValue, object newValue)
{
if (oldvalue == newValue)
return;
....
RebindData(dataGrid);
}