Answered by:
generate treeviewItem

Question
-
Hello,
i want this
+TextBox
TextBox
Textbox
I have the solution for the first part
+TextBox
TextBox
But in want push the Enterkey and ia have this
+TextBox
TextBox
Textbox
Here the xml
<TreeView Name="trvMenu" TreeViewItem.Expanded="TreeViewItem_Expanded" Height="200" HorizontalAlignment="Left" Margin="12,48,0,0" VerticalAlignment="Top" Width="120" ItemsSource="{Binding}"> <TreeView.Resources> <Style TargetType="TreeViewItem"> <EventSetter Event="KeyUp" Handler="treeViewItem_KeyUp" /> </Style> </TreeView.Resources> <TreeView.ItemTemplate> <HierarchicalDataTemplate DataType="{x:Type TreeView}" ItemsSource="{Binding Items}"> <TextBox></TextBox> </HierarchicalDataTemplate> </TreeView.ItemTemplate> </TreeView>
and the C#:
public void TreeViewItem_Expanded(object sender, RoutedEventArgs e) { TreeViewItem childItem1 = new TreeViewItem(); TextBox txt = new TextBox(); childItem1.Items.Add(txt); private void treeViewItem_KeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { Handler(sender, e); Debug.WriteLine(" Enter"); TextBox txt = new TextBox(); item.Items.Add(txt); //thats is a error! }
I want not a new treeviewitem i want in the treeviewitem only a new texbtbox
Wednesday, October 28, 2015 12:23 AM
Answers
-
You cannot add items to the Items collection of the TreeView when its ItemsSource property has been set or been bound to a source collection. Then you will get an InvalidOperationException. You use either ItemsSource or the Items collection but not both.
If you change the type of the ItemsSource collection that you are binding to to an ObservableCollection<object> in your DataContext/ViewModel, you could add the TextBox to this collection instead of adding it to the Items collection of the TreeView. Just cast the ItemsSource property to an ObservableCollection<object>:
private void treeViewItem_KeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { TextBox txt = new TextBox(); ObservableCollection<object> sourceCollection = trvMenu.ItemsSource as ObservableCollection<object>; sourceCollection.Add(txt); //thats is a error! } }
Again, this requires you to change the type of the source collection that is bound to the ItemsSource property of the TreeView to ObservableCollection<object> (or ObservableCollection<TextBox> since you are adding TextBoxes to it).
Hope that helps.Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
- Proposed as answer by Xavier Xie-MSFT Thursday, October 29, 2015 3:01 AM
- Marked as answer by Xavier Xie-MSFT Saturday, November 7, 2015 4:56 AM
Wednesday, October 28, 2015 5:04 PM -
Then you haven't changed the type of the source collection to ObservableCollection<object> as I told you to do. Once again: The source collection that the ItemsSource property of the TreeView is bound to must be an ObservableCollection<object>. Otherwise the following cast will fail and you will get a NullReferenceException when trying to use the sourceCollection reference:
ObservableCollection<object> sourceCollection = trvMenu.ItemsSource as ObservableCollection<object>;
Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question.
- Marked as answer by Xavier Xie-MSFT Saturday, November 7, 2015 4:56 AM
Thursday, October 29, 2015 4:19 PM
All replies
-
>I want not a new treeviewitem i want in the treeviewitem only a new texbtbox
Well you can't do that.
You also need to read up on hierarchicaldatatemplate. It should have a datatype matching the type of something presented via Items rather than treeview.
.
This is a bad way to use a treeview anyhow.
Adding stuff manually on expansion is a bad idea.
You should use hierarchicaldatatemplate and itemtemplate, bind to a collection of objects, which in turn have some property containing children.
http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode
Wednesday, October 28, 2015 9:28 AM -
Hello thank you for your answer.
the Problem is i dont want youse a treeview as a treeview, i use it as a tool to create a file what i need in a other programm the same as xml
the user goes to the first point
and wite in TextBox: Hallo
in the richtextbox is see <hallo> <hallo>
then he goes in the child in wirte "1", and he see <hallo><1>hallo> and click enter,
then he write "2" in the created TextBox by push enter
an he see in the box <hallo><1><2><hallo> and then he click new menü an wirte "good day" an in the box you see
<hallo><1><2><hallo>
<good day><good day>
that is what i wann do
Wednesday, October 28, 2015 10:22 AM -
Anything you want to do with a treeview can be done using mvvm.
The user does something in one treeviewitem.
That is bound to some object which exposes a command or a property.
You click your menu or whatever.
The viewmodel object can add an item to it's observable collection which represents the children.
The view responds and you have a new node appears in the view.
.
As Josh Smith explains in that article I linked.
Working with the WPF treeview like you do in Winforms and adding controls is a bad idea.
- Edited by Andy ONeill Wednesday, October 28, 2015 2:06 PM
Wednesday, October 28, 2015 11:08 AM -
You cannot add items to the Items collection of the TreeView when its ItemsSource property has been set or been bound to a source collection. Then you will get an InvalidOperationException. You use either ItemsSource or the Items collection but not both.
If you change the type of the ItemsSource collection that you are binding to to an ObservableCollection<object> in your DataContext/ViewModel, you could add the TextBox to this collection instead of adding it to the Items collection of the TreeView. Just cast the ItemsSource property to an ObservableCollection<object>:
private void treeViewItem_KeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { TextBox txt = new TextBox(); ObservableCollection<object> sourceCollection = trvMenu.ItemsSource as ObservableCollection<object>; sourceCollection.Add(txt); //thats is a error! } }
Again, this requires you to change the type of the source collection that is bound to the ItemsSource property of the TreeView to ObservableCollection<object> (or ObservableCollection<TextBox> since you are adding TextBoxes to it).
Hope that helps.Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
- Proposed as answer by Xavier Xie-MSFT Thursday, October 29, 2015 3:01 AM
- Marked as answer by Xavier Xie-MSFT Saturday, November 7, 2015 4:56 AM
Wednesday, October 28, 2015 5:04 PM -
Thank you
But i get a System.NullReferenceException by
sourceCollection.Add(txt)
Thursday, October 29, 2015 3:27 PM -
Then you haven't changed the type of the source collection to ObservableCollection<object> as I told you to do. Once again: The source collection that the ItemsSource property of the TreeView is bound to must be an ObservableCollection<object>. Otherwise the following cast will fail and you will get a NullReferenceException when trying to use the sourceCollection reference:
ObservableCollection<object> sourceCollection = trvMenu.ItemsSource as ObservableCollection<object>;
Please remember to close your threads by marking all helpful posts as answer and then start a new thread if you have a new question.
- Marked as answer by Xavier Xie-MSFT Saturday, November 7, 2015 4:56 AM
Thursday, October 29, 2015 4:19 PM