假设有一个前向链表:
public class A
{
public B Next { get; set; }
public String Value { get; set; }
public A(string value, B next)
{
this.Next = next;
this.Value = value;
}
}
public class B
{
public A Next { get; set; }
public int Value { get; set; }
public B(int value, A next)
{
this.Next = next;
this.Value = value;
}
}
public partial class MyWindow : Window
{
public A My_List{ get; set; }
public MyWindow()
{
Program program = new Program();
this.My_List = new A("value1", new B(1, new A("Value2", new B(2, new A("value3", new B(3, null))))));
this.DataContext = this;
InitializeComponent();
}
}
如何在 Wpf 中显示 *My_List*?我试过:
<StackPanel>
<StackPanel.Resources>
<DataTemplate DataType="{x:Type WpfApplication2:A}" x:Key="A_Template">
<StackPanel Orientation="Horizontal">
<Label Content="{Binding Path=Value}" />
<Label Content="---" />
<Label Content="{Binding Path=Next.Value}" />
<ItemsControl ItemsSource="{Binding Path=Next.Next}" ItemTemplate="{DynamicResource A_Template}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
</DataTemplate>
</StackPanel.Resources>
<ItemsControl ItemsSource="{Binding Path=My_List}"
ItemTemplate="{StaticResource A_Template}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</StackPanel>
但它不起作用,我认为这是因为 *My_List* 必须是实际列表而不是第一个元素。
编辑:只有一个空窗口,没有输出,没有编译器错误,也没有运行时异常。