में आइटम जोड़ते समय TreeTiew के लिए आइटम टेम्पलेट का उपयोग करना मैं मैन्युअल रूप से कोड में TreeViewItems जोड़ रहा हूं और उन्हें प्रदर्शित करने के लिए डेटा टेम्पलेट का उपयोग करना चाहता हूं लेकिन यह पता नहीं लगा सकता कि कैसे। मैं ऐसा कुछ करने की उम्मीद कर रहा हूं लेकिन आइटम खाली हेडर के रूप में प्रदर्शित होते हैं। मैं क्या गलत कर रहा हूं?कोड
XAML
<Window x:Class="TreeTest.WindowTree"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="WindowTree" Height="300" Width="300">
<Grid>
<TreeView Name="_treeView">
<TreeView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" />
<TextBlock Text="{Binding Path=Age}" />
</StackPanel>
</DataTemplate>
</TreeView.ItemTemplate>
</TreeView>
</Grid>
</Window>
कोड के पीछे
using System.Windows;
using System.Windows.Controls;
namespace TreeTest
{
public partial class WindowTree : Window
{
public WindowTree()
{
InitializeComponent();
TreeViewItem itemBob = new TreeViewItem();
itemBob.DataContext = new Person() { Name = "Bob", Age = 34 };
TreeViewItem itemSally = new TreeViewItem();
itemSally.DataContext = new Person() { Name = "Sally", Age = 28 }; ;
TreeViewItem itemJoe = new TreeViewItem();
itemJoe.DataContext = new Person() { Name = "Joe", Age = 15 }; ;
itemSally.Items.Add(itemJoe);
_treeView.Items.Add(itemBob);
_treeView.Items.Add(itemSally);
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
}
}
आपके दोनों समाधान काम करते हैं। आपको वापस आने के लिए बहुत लंबा समय लगता है। मैं अंत में एक पेड़ का उपयोग नहीं कर पाया, इसके बजाय एक कस्टम पदानुक्रमित सूची बॉक्स लागू किया। –
मैट के लिए हुरेय! बस मुझे क्या चाहिए (दूसरा समाधान) –