Dobrý deň. Poradí mi niekto, prosím, ako môžem v nižšie uvedenom programe priradiť podzložku(subItem) ku zložke v TreeView (item), na ktorú urobím dvojklik? Ďakujem.
<Window x:Class="WpfTreViewSubItem.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView MouseDoubleClick="TreeView_MouseDoubleClick">
<TreeViewItem Header="Jeden" Tag= "10" IsExpanded="True"/>
<TreeViewItem Header="Dva" Tag="20" IsExpanded="True">
<TreeViewItem Header="A" Tag="21 " IsExpanded="True">
<TreeViewItem Header="Alfa" Tag="211" IsExpanded="True"/>
</TreeViewItem>
<TreeViewItem Header="B" Tag =" 22" IsExpanded="True"/>
</TreeViewItem>
<TreeViewItem Header="Tri" Tag="30" IsExpanded="True"/>
</TreeView>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace WpfTreViewSubItem
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TreeView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
// ... Create a TreeViewItem.
TreeViewItem item = new TreeViewItem();
item.Header = "Computer";
item.Tag = 1;
TreeViewItem subItem = new TreeViewItem();
subItem.Header = "Risk";
TreeViewItem subItem1 = new TreeViewItem();
subItem1.Header = "Riziko";
subItem.Items.Add(subItem1);
item.Items.Add(subItem);
// ... Create a second TreeViewItem.
TreeViewItem item2 = new TreeViewItem();
item2.Header = "PC";
item2.ItemsSource = new string[] { "Monitor", "CPU", "Mouse", "Bluethooth" };
// ... Get TreeView reference and add both items.
var tree = sender as TreeView;
var itemTag = tree.SelectedItem as TreeViewItem;
MessageBox.Show("Header: " + itemTag.Header.ToString() + " Tag: " + itemTag.Tag.ToString());
//?????
//Tu niekde by to mohlo byť Foreach items in TreeView....
// if( header== alebo Tag ==
//?????
tree.Items.Add(item);
tree.Items.Add(item2);
}
}
}
|