0

假设有一个前向链表:

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* 必须是实际列表而不是第一个元素。

编辑:只有一个空窗口,没有输出,没有编译器错误,也没有运行时异常。

4

1 回答 1

0

如何在 Wpf 中显示 *My_List*?

如果你想A用作ItemsSourcefor ItemsControl,那么你必须像这样IEnumerable实现A

public class A : IEnumerable
{
    public B Next { get; set; }

    public String Value { get; set; }

    public A(string value, B next)
    {
        this.Next = next;
        this.Value = value;
    }

    #region IEnumerable Members

    private class AEnumerator : IEnumerator
    {
        private readonly A head;

        public AEnumerator(A head)
        {
            this.head = head;
        }

        #region IEnumerator Members

        public object Current { get; private set; }

        public bool MoveNext()
        {
            if (Current == null)
            {
                Current = head;
            }
            else
            {
                if (Current.GetType() == typeof(A))
                {
                    Current = ((A)Current).Next;
                }
                else
                {
                    Current = ((B)Current).Next;
                }
            }

            return Current != null;
        }

        public void Reset()
        {
            throw new NotImplementedException();
        }

        #endregion
    }

    public IEnumerator GetEnumerator()
    {
        return new AEnumerator(this);
    }

    #endregion
}
于 2013-01-30T07:29:56.943 回答