I have a TreeView in which I would like to allow the user to add and delete subitems from. In exploring basic functionality I am using a button and a textbox to add this subitem. When the user clicks on the button a new TreeViewItem needs to be created and set as a subitem of my parent TreeView with the text from the textbox set as the subitem's Header.
This is my current code under the button_click event:
//ADD T_ITEM TO PARENT TREEVIEW
private void button1_Click(object sender, RoutedEventArgs e)
{
TreeViewItem item = new TreeViewItem();
item.Header = textBox1.Text;
//Compiler does not recognize "Nodes"
Parent.Nodes.Add(item);
}
Specifically, the compiler has a problem with Nodes. The main question that I've used to help me makes a lot of sense, but just doesn't work for me. All of the sources I have looked at uses the Nodes command at one time or another with no problem. Do I need to include a reference, or is my code completely off?
--This guide uses System.Windows.Forms; in order to use Nodes, but doesn't seem to help because I am using Windows Presentation Foundation.
Please show me how to get my code working in the right direction.
Thank you.
I did some more research and found the equivalent method for adding child TreeViewItems to parent TreeViewItems in WPF.
This is the change I made to my code:
//ADD T_ITEM TO PARENT TREEVIEW
private void button1_Click(object sender, RoutedEventArgs e)
{
TreeViewItem item = new TreeViewItem();
item.Header = textBox1.Text;
Parent.Items.Add(item);
}
Related
I'm kinda new to this and after searching, and trying things i still couldn't achieve what i wanted, so here`s my question. In a windows form application i have a treeview in which i add custom treenodes (the custom node extends the treenode class and the additional variable is of type Image). So, everytime i select a node i want its image (after i have loaded the image to the node, of course) to be displayed in the picturebox. I have tried the afterselecet event handler and the mouse click event handler, but still the image was not displayed. This is the final piece of code after a lot of experimenting :
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
pictureBox1.Image = ((MyNode)(treeView1.SelectedNode)).MyImage;
}
I'm sure i am missing something here but don't know what exactly. Any help would be appreciated! Thank You!
I am using a wpf tree in a .net form. So, I don't have any xaml. I simply do everything in code. I am using Hierarchical Data Template to bind my data to the wpftree.
I am trying to find a way to get the TreeViewItem for the Node selected in the tree. I tried registering a EventHandler on the SelectedItemChanged event on the TreeView, but in that handler I only get the associated data object. Since my tree is virtual, ItemContainerGenerator.ContainerFromItem doesn't work.
When I searched on StackOverflow, one suggestion was to listen to the TreeViewItem.Selected event.
But I couldn't find a way to do this in code. ( I don't have a xaml).
Any help is greatly appreciated.
thank you.
What you could do is attach the handler to each control every time you add it
void AddTreeViewItem()
{
TreeView t = new TreeView();
TreeViewItem treeItem = new TreeViewItem();
t.Items.Add(treeItem);
treeItem.Selected += DoSomethingHere;
}
private void DoSomethingHere(object sender, RoutedEventArgs e)
{
Console.WriteLine("Tree Item Selected");
}
if I add control in Microsoft Blend 4 without set Name to this control and I want to set name to it and use it in c# how ?
example I added button using Blend in my layout but without give it a name
I want to give it a name using c# without x:Name="" in xaml
In your place I would give LogicalTreeHelper.GetChildren (this) a chance. It returns a collection of children to Window (this is a handle to Window) Reference MSDN
From there you can try to find your control.
But I think it is easier to try to rewrite the control (or look for another component) so you can have names on the children. That was your problem from the start.
Hope it helps
Gorgen
First, why in the world would you want to do that?
If you do not set a name you have no easy way of accessing the control. However you can get access to the control via relationships to other controls or events that pass a reference, for example the loaded event.
e.g.
private void Menu_Loaded(object sender, RoutedEventArgs e)
{
(sender as Menu).Name = "MainMenu";
}
Or if the control is the child of another control:
(ControlStack.Children[0] as Menu).Name = "MainMenu";
But i cannot think of anything useful that could be achieved by that...
You probably just want to get a reference to the object which you can easily store in a class member. In some cases you can also slice up your XAML using resources.
e.g.
<local:SomethingIWouldLikeToReference x:Key="SomethingIWouldLikeToReference"/>
<local:UserControl x:Name="userControl">
<Stuff>
<MoreStuff Content="{StaticResource SomethingIWouldLikeToReference}"/>
</Stuff>
</local:UserControl>
public MainWindow()
{
InitializeComponent();
MyReference = FindResource("SomethingIWouldLikeToReference") as SomethingIWouldLikeToReference;
}
Example if I have ListView Control and I want to use it to add items and remove items
Make private ListView and initialize it
ListView temp_control_List = new ListView()
then make loaded Eventhandler from Blend so it will be in VS then
private void ListView_Loaded(object sender, System.Windows.RoutedEventArgs e)
{
temp_control_List = sender as ListView;
}
Now you can add and remove to and from the list view control from temp_control_List
I created Drag&Drop mechanism for my TreeView. I added DragEnter, DragDrop and ItemDrag methods and everything works fine.
But when you are doing D&D with standard Windows controls, destination node is highlighted.
Image is worth 1000 words, video probably even more:
http://www.youtube.com/watch?v=PlltSiihHPo
I mean such highlight effect like you can see in this video on Recycle Bin.
That's not a TreeView, it's a ListView with View = LargeIcons. TreeView isn't a great control as a drop target since it hides sub-nodes. But you can solve both problems by implementing the DragOver event. Test where the mouse is at and expand and select the node:
void treeView1_DragOver(object sender, DragEventArgs e) {
var pos = treeView1.PointToClient(new Point(e.X, e.Y));
var hit = treeView1.HitTest(pos);
if (hit.Node != null) {
hit.Node.Expand();
treeView1.SelectedNode = hit.Node;
}
}
I am using ASP.NET with C# 2.0 and Visual Studio 2005. I am using a Master page and content pages. I have a treeview menu in the master page and when a user selects any menu item I redirect to that content page.
My problem is that after a user navigates to the content page all the treenodes refresh and the structure is collapsed. I want the selected treenode to stay expanded.
Can anybody help me out?
When you refresh the treeview you want to call treeView1.ExpandAll();
Also add an event for the BeforeCollapse and set the event's Cancel property to true, to prevent the user from collapsing your treenodes.
private void treeView1_BeforeCollapse(object sender, TreeViewCancelEventArgs e)
{
e.Cancel = true;
}
Hope this helps.
-jeremy
This is a common enough problem that is automatically handled by ASP.NET if you use a SiteMapDataSource control as the datasource for your TreeView. In this case, you haven't mentioned what the Datasource of your TreeView is.
You also haven't mentioned if the TreeView contains links (the NavigateUrl property is set) or Text items that postback for each click. If it is the former, then as far as I know, you are out of luck! This is because none of the Selection events are raised for TreeNodes which have their NavigateUrl set. They just function as regular hyperlinks.
If however, it is the latter, then you can try out the following steps :
a. Handle the SelectedNodeChanged event of the TreeView. In this event handler, retrieve the current value of the SelectedNode.ValuePath property and store it in ViewState/Session. Use the Value of the of the SelectedNode to conditionally redirect the page to URL mapped to it.
Something like the following:
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
TreeNode selNode = TreeView1.SelectedNode;
string pathToNode = selNode.ValuePath;
Session.Add("SelPath", pathToNode);
switch (selNode.Value)
{
//Redirect to URL accordingly.
}
}
b. On subsequent load of the Master page (the page to which you redirected), retrieve the value of the ValuePath set earlier and find the previously Selected node and Expand it.
Something like the following:
protected void Page_Load(object sender, EventArgs e)
{
if (Page.IsPostBack)
{
string pathToNode = (string)Session("SelPath");
Session.Remove("SelPath");
TreeNode selNode = TreeView1.FindNode(pathToNode);
if (selNode != null)
{
selNode.Expand();
}
}
}
Note that I haven't had an opportunity to test the code so this is mostly hypothetical.
Try using the OnTreeNodeDataBound event and the treeView.SelectedNode property
Also, might want to check how/ when you're binding your TreeView to it's DataSource. You might be rebinding it on IsPostBack which will re-render the tree.
The TreeView should maintain its nodes on PostBack.
Even though you are using a Master page, once the user navigates to the content page it is rendered as a new/different page. Because of the Master page the same treeview is loaded but not the same instance. You will need to store and load what nodes were expanded.