我知道使用WPF在C#中编程与传统的C#程序不同,因此大多数在线资料都没有说明我需要的内容.
我的WPF窗口中有一个TreeView控件,其中有父节点和子节点.我想将它们存储在Node类型的列表中(id,name,parent).

我使用这个得到了所选项目/节点的名称:
private void TreeViewItem_OnItemSelected(object sender, RoutedEventArgs e)
{
TreeViewItem item = treeView.SelectedItem as TreeViewItem;
nameTxt.Text = item.Header.ToString();
}
Run Code Online (Sandbox Code Playgroud)
我尝试在使用它之前立即获取子节点的Parent:
TreeViewItem item = treeView.SelectedItem as TreeViewItem;
nameTxt.Text = item.Parent.ToString();
Run Code Online (Sandbox Code Playgroud)
但是,这将返回根父(A)而不是子父(即2).
我应该做些什么来改变孩子的直接父母而不是根父母?:)
编辑:这是XAML
<TreeView Name="treeView" HorizontalAlignment="Left" Height="564" Margin="10,68,0,0" VerticalAlignment="Top" Width="363">
<TreeViewItem TreeViewItem.Selected="TreeViewItem_OnItemSelected" Header="A" IsExpanded="True" Height="554" FontSize="18">
<TreeViewItem Header="1" />
<TreeViewItem Header="2" />
</TreeViewItem>
</TreeView>
Run Code Online (Sandbox Code Playgroud) 我有一个“活动”类型的列表。我需要找到该列表中出现次数最多的元素。例如:
Activity a = new Activity();
a.Location = "USA";
Activity b = new Activity();
b.Location = "Sri Lanka";
Activity b = new Activity();
b.Location = "USA";
List<Activity> activityList = new List<Activity>();
activityList.add(a);
//......adding the rest of the objects as well.
Run Code Online (Sandbox Code Playgroud)
现在我需要找到这个列表中出现次数最多的位置。例如,上例中出现次数最多的 Location 是:USA。
我试过这个:
String currentLocation = "";
String mostOccurring = "";
int currentCount = 0;
int highest = 0;
for (int i = 0; i < activityList.Count; i++)
{
currentLocation = activityList[i].Location;
foreach (Activity activity in activityList)
{
if …Run Code Online (Sandbox Code Playgroud)