This is a migrated thread and some comments may be shown as answers.

Error While Adding New Item to RadTreeViewItem's Datacontext

3 Answers 26 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Hamed
Top achievements
Rank 2
Hamed asked on 07 Jul 2014, 08:39 AM
hi there,

I've got a problem in adding some Data items to RadTreeView's DataContext. Here is the code and descriptions:

in My XAML side, I've added a RadTreeView like this:

<telerik:RadTreeView x:Name="trvGeoDevisions" Grid.Row="1" Margin="0,15,0,0" HorizontalContentAlignment="Stretch" IsVirtualizing="True" IsExpandOnSingleClickEnabled="True"><br>            <telerik:RadTreeViewItem x:Name="TrootItem"  IsExpanded="True"><br>                <telerik:RadTreeViewItem.Header><br>                    <StackPanel><br>                        <TextBlock Text="Geo Devisions"/><br>                        <telerik:RadWatermarkTextBox x:Name="txtNewCountry" WatermarkContent="New Country..." Style="{StaticResource tk}" KeyDown="txtNewCountry_KeyDown"/><br>                    </StackPanel><br>                </telerik:RadTreeViewItem.Header><br>                <telerik:RadTreeViewItem.ItemTemplate><br>                    <HierarchicalDataTemplate ItemsSource="{Binding GeographicalDevisions}"><br>                        <Grid Margin="0,10,0,0"><br>                            <StackPanel><br>                                <TextBlock Text="{Binding Path=Name}" /><br>                                <telerik:RadWatermarkTextBox x:Name="txtNewEntry" WatermarkContent="{Binding Path=Type,Converter={StaticResource wp},ConverterParameter='New 1...'}" Visibility="{Binding Type.Types.Count,Converter={StaticResource numberToVisiblity}}" Style="{StaticResource tk}" KeyDown="txtNewEntry_KeyDown" <br>                                                             Tag="{Binding Mode=OneWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type telerik:RadTreeViewItem}}}" /><br>                            </StackPanel><br>                        </Grid><br>                    </HierarchicalDataTemplate><br>                </telerik:RadTreeViewItem.ItemTemplate><br><br>            </telerik:RadTreeViewItem><br>        </telerik:RadTreeView><br>

I want to add some Items when user types something in RadWatermark and presses Enter key, Thus I Write the following code:
 Private Sub txtNewEntry_KeyDown(sender As Object, e As KeyEventArgs)<br>        If e.Key = Key.Enter Then<br>            Dim control = CType(sender, Telerik.Windows.Controls.RadWatermarkTextBox)<br>            Dim GeoDev = CType(control.DataContext, GeographicalDevision)<br>            Dim tag As Telerik.Windows.Controls.RadTreeViewItem = control.Tag<br><br>            Dim nEntry As New GeographicalDevision With {.Name = control.Text, .ParentGeographicalDevision = GeoDev, .Type = GeoDev.Type.Types(0)}<br>            GeoDev.GeographicalDevisions.Add(nEntry)<br><br>            control.Clear()<br>        End If<br>    End Sub

The Sub runs successfully but right after End Sub, I got this Error in Sub Main of Application:

​Object reference not set to an instance of an object.
Source: Telerik.Windows.Controls.Navigation
HResult: -2147467261
StackTrace:    at Telerik.Windows.Controls.TreeView.TreeViewPanel.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)
   at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)
   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   at System.Windows.UIElement.Measure(Size availableSize)
   at System.Windows.ContextLayoutManager.UpdateLayout()
   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   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.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(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.LegacyInvokeImpl(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 Shoniz.HRMS.HRMSApplication.Application.Main() in E:\6. HRMS Application\HRMSApplication\HRMSApplication\obj\Debug\Application.g.vb:line 77
   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.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()


WHAT'S WRONG WITH ME?

3 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 08 Jul 2014, 10:15 AM
Hello Hamed,

I tested your code, but I wasn't able to reproduce the reported exception. This is why I prepared a project with the code snippets from your reply. Can you please take a look at it and let me know if I am missing something?

Thank you for any help you can provide.

Regards,
Martin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Hamed
Top achievements
Rank 2
answered on 10 Jul 2014, 10:24 AM
THANK YOUT FOR YOUR ATTENTION

The Only matter is that my GeographicalDevision is not a ViewModel, It's the Model itself. i mean it's a class generated by EF's EDMX. can it affect?
0
Martin Ivanov
Telerik team
answered on 14 Jul 2014, 03:35 PM
Hi Hamed,

Without your implementation I cannot be sure what is causing the reported exception. This is why I would recommend you to open a new Support Ticket and attach a sample project, or  send me code snippets with an isolated implementation that recreates the issue. This will allow me to test it locally and investigate what is causing this behavior.

Also keep in mind that, when you use the RadTreeView, it is not recommended to mix data binding and static RadTreeViewItems, because of the following facts:
  • By design the UI Virtualization of the RadTreeView doesn't work with static declared RadTreeViewItems.
  • The default drag and drop behavior won't work correctly when you have both static RadTreeViewItems and business objects. 
  • The tree view (and tree view items) style won't be applied correctly.

In addition, can you please try to data bind the entire treeview (to remove or replace the root item with business object) and let me know you still reproduce the exception?

Thank you in advance for your cooperation.

Regards,
Martin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
TreeView
Asked by
Hamed
Top achievements
Rank 2
Answers by
Martin Ivanov
Telerik team
Hamed
Top achievements
Rank 2
Share this question
or