Radio sam nešto slično pre par godina pa ću izvući nekoliko bitnijih delova koda.
TreeView sam popunjavao iz XML-ova, u zavisnosti od odabrane grupe komandi što bi bilo slično prikazu MenuItem-a izabranog MenuItem-a iz prvog reda.
Code:
private void PuniGlTreeView(string grupa)
{
OdabranaGrupaKomandi = grupa;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
XmlDataProvider provider = new XmlDataProvider();
switch (grupa)
{
case "Unosi":
doc.Load(AppDomain.CurrentDomain.BaseDirectory + "\\OstaliResursi\\TreeViewItemsUnosi.xml");
provider.Document = doc;
provider.XPath = "/Unosi/Nivo1";
this.GlTreeView.SetBinding(TreeView.ItemsSourceProperty, new Binding() { Source = provider });
break;
case "Izvestaji":
doc.Load(AppDomain.CurrentDomain.BaseDirectory + "\\OstaliResursi\\TreeViewItemsIzvestaji.xml");
provider.Document = doc;
provider.XPath = "/Izvestaji/Nivo1";
this.GlTreeView.SetBinding(TreeView.ItemsSourceProperty, new Binding() { Source = provider });
break;
case "Obrade":
doc.Load(AppDomain.CurrentDomain.BaseDirectory + "\\OstaliResursi\\TreeViewItemsObrade.xml");
provider.Document = doc;
provider.XPath = "/Obrade/Nivo1";
this.GlTreeView.SetBinding(TreeView.ItemsSourceProperty, new Binding() { Source = provider });
break;
case "Obracuni":
doc.Load(AppDomain.CurrentDomain.BaseDirectory + "\\OstaliResursi\\TreeViewItemsObracuni.xml");
provider.Document = doc;
provider.XPath = "/Obracuni/Nivo1";
this.GlTreeView.SetBinding(TreeView.ItemsSourceProperty, new Binding() { Source = provider });
break;
default:
break;
}
GlTreeView.Focus();
}
Izvršavanje komande je moguće klikom na TreeView ili pritiskom na taster <ENTER>.
Code:
private void GlTreeView_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
e.Handled = true;
IzvrsiKomandu();
}
}
private void GlTreeView_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
try
{
if (((System.Xml.XmlElement)GlTreeView.SelectedItem).HasChildNodes == false)
{
IzvrsiKomandu();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
Primeti da je u slučaju kada je TreeView bindovan za XML, TreeView.SelectedItem u stvari XmlElement.
Metoda IzvrsiKomandu, u zavisnosti od odabrane vrste komandi i naziva izabranog Item-a upućuje na otvaranje nekog prozora (ili bilo šta drugo).
Code:
private void IzvrsiKomandu()
{
if (GlTreeView.SelectedItem != null)
{
string OdabranaKomanda = ((System.Xml.XmlElement)GlTreeView.SelectedItem).Attributes["Name"].Value.ToString();
switch (OdabranaGrupaKomandi)
{
case "Unosi":
IzvrsiKomanduUnosi(OdabranaKomanda);
break;
case "Izvestaji":
IzvrsiKomanduIzvestaji(OdabranaKomanda);
break;
case "Obrade":
IzvrsiKomanduObrade(OdabranaKomanda);
break;
case "Obracuni":
IzvrsiKomanduObracuni(OdabranaKomanda);
break;
default:
break;
}
}
}
private void IzvrsiKomanduUnosi(string komanda)
{
switch (komanda)
{
case "Vlasnik" :
ProzorVlasnik();
break;
case "Organizacione jedinice":
ProzorOrganizacioneJedinice();
break;
case "Podaci o zaposlenima" :
ProzorZaposleni();
break;
default:
MessageBox.Show("Odaberite komandu.");
break;
}
}
private void ProzorVlasnik()
{
Vlasnik v = new Vlasnik();
v.Inicijalizacija();
v.ShowDialog();
}
Nadam se da će ovo pomoći.
Pozdrav.