在EditingTemplateCell中使用Popup:Message = Value不能为null。参数名称:后代

时间:2010-09-22 15:24:37

标签: wpf

我正在使用WPF DataGrid,我放了一个DataGridTemplateColumn。因为列应该对复杂类型(它是一个对象)执行编辑,所以我决定放置一个打开弹出窗口的togglebutton。代码如下:

                <DataTemplate>
                    <Grid>
                        <ToggleButton Grid.Column="2" x:Name="tb" Focusable="false" IsChecked="False" ClickMode="Press"  Content="Edit values" />
                        <Popup IsOpen="{Binding ElementName=tb, Path=IsChecked, Mode=OneWay}" Placement="Bottom" x:Name="Popup" Focusable="False" StaysOpen="False" AllowsTransparency="True"  Margin="0,1,0,0">
                            <Grid x:Name="dd" SnapsToDevicePixels="True">
                                <Border x:Name="ddb" Margin="0,-1,0,0" BorderBrush="DarkGray"  BorderThickness="1" CornerRadius="0,0,3,3" Background="Gray">
                                    <Grid>
                                    <Grid.RowDefinitions>
                                        <RowDefinition />
                                        <RowDefinition />
                                        <RowDefinition />
                                    </Grid.RowDefinitions>
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition />
                                        <ColumnDefinition />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Text="Uno:" Grid.Row="0" Grid.Column="0" />
                                    <TextBox Text="{Binding Path=calendar.uno, Mode=TwoWay, StringFormat={}{0:0.##}}" Grid.Row="0" Grid.Column="1" />
                                    <TextBlock Text="Due:" Grid.Row="1" Grid.Column="0" />
                                        <TextBox Text="{Binding Path=calendar.due, Mode=TwoWay, StringFormat={}{0:0.##}}" Grid.Row="1" Grid.Column="1" />
                                    <TextBlock Text="Tre:" Grid.Row="2" Grid.Column="0" />
                                        <TextBox Text="{Binding Path=calendar.tre, Mode=TwoWay, StringFormat={}{0:0.##}}" Grid.Row="2" Grid.Column="1" />
                                    </Grid>
                                </Border>
                            </Grid>
                        </Popup>
                    </Grid>
                </DataTemplate>
            </DataGridTemplateColumn.CellEditingTemplate>

弹出窗口有效,但通常会发生以下异常:

System.ArgumentNullException was unhandled
  Message=Value cannot be null.
Parameter name: descendant
  Source=PresentationCore
  ParamName=descendant
  StackTrace:
       at System.Windows.Media.VisualTreeHelper.IsAncestorOf(DependencyObject ancestor, DependencyObject descendant, Type stopType)
       at System.Windows.Controls.DataGridCell.RemoveBindingExpressions(BindingGroup bindingGroup, DependencyObject element)
       at System.Windows.Controls.DataGridCell.BuildVisualTree()
       at System.Windows.Controls.DataGridCell.OnIsEditingChanged(Boolean isEditing)
       at System.Windows.Controls.DataGridCell.OnIsEditingChanged(Object sender, DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e)
       at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args)
       at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType)
       at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
       at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Boolean value)
       at System.Windows.Controls.DataGridCell.BeginEdit(RoutedEventArgs e)
       at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(ExecutedRoutedEventArgs e)
       at System.Windows.Controls.DataGrid.OnExecutedBeginEdit(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
       at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
       at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
       at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)
       at System.Windows.Input.RoutedCommand.Execute(Object parameter, IInputElement target)
       at System.Windows.Controls.DataGrid.BeginEdit(RoutedEventArgs editingEventArgs)
       at System.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDown(MouseButtonEventArgs e)
       at System.Windows.Controls.DataGridCell.OnAnyMouseLeftButtonDownThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
       at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at WpfApplication5.App.Main() in C:\Users\r.sarati\Desktop\WpfDataGridSynchronized_V1.00\WpfDataGridSynchronized\CF40\WpfApplication5\obj\Debug\App.g.cs:line 50
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

有什么问题? (使用多列不是有效的替代方法)

1 个答案:

答案 0 :(得分:1)

目前我的应用中也有这个例外;相同的堆栈跟踪。我发现这方面的解决方案也很少,但这些解决方案并不是很“好”:

1 我在整个对话框中使用了aero样式,我在其中使用DataGrid来查看不同Windows版本中的默认控件(主要用于XP以查看控件“更好”)。当我在对话框的xaml中发表以下部分代码时,没有显示其他异常:

<ResourceDictionary.MergedDictionaries>
       <ResourceDictionary Source="/PresentationFramework.Aero, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
</ResourceDictionary.MergedDictionaries>

但我现在在XP上,我必须检查Win7上是否也解决了这个问题。 编辑:也适用于Windows7。

2 请尝试在微软页面上查看此链接:http://support.microsoft.com/kb/2498911

3 由于它也是在microsoft页面上编写的,你仍然可以关闭数据网格的行或列的虚拟化,但如果你有1000行,则需要一些时间来加载所有内容:/ < / p>

编辑:

4. 我下载了Aero.NormalColor.xaml(这里有一些主题来自microsoft:http://msdn.microsoft.com/en-us/library/aa970773.aspx)并将此主题添加到我的app.xaml到app词典中。这似乎解决了这个问题(可能看起来相同但在这个主题的代码中存在一些差异)。

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Themes/GlassButton.xaml"/>
            <ResourceDictionary Source="Themes/Aero.NormalColor.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>