0

我在 texblocknumber.text="123" 中有一个值。当我每次按下按钮时,我想将值推送到堆栈中,然后在列表视图中显示堆栈值。我在上面的代码中有两个错误1.我想在stack.Push(texblocknumber.text)中添加一个texblocknumber.text;2. 我想在列表视图中添加一个堆栈.push 值

<ListBox Margin="0,0,-12,0" x:Name="mylist1">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <StackPanel>
                            <TextBlock x:Name="texblocknumber"  Text="{Binding datetime}" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                            <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                                <TextBlock x:Name="txt2"   Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>

                            </StackPanel>
                        </StackPanel>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

c# 代码

    static Stack<string> GetStack()
    {

        Stack<string> stack = new Stack<string>();

        stack.Push("fghj");
        stack.Push("bnmc");
        return stack;
    }
4

2 回答 2

0

您可以使用按钮的绑定并使用Observable 集合作为列表的源而不是堆栈,因为它实现了 INotifycollectionchanged

于 2013-07-17T07:30:05.687 回答
0
just check this solution what i am doing in it on btn click i am saving content on a observablecollection..and simulataneously i have binded it to listbox..(i am showing with collection of string..you can do it with collection of objects too)..

你的列表框..

<ListBox Margin="0,0,-12,0" ItemsSource="{Binding ListOFText}" x:Name="mylist1" VerticalAlignment="Top" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock x:Name="texblocknumber"  Text="{Binding }" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                        <StackPanel Orientation="Horizontal" Margin="0,0,0,17">
                            <TextBlock x:Name="txt2"   Margin="5,0,0,0" Style="{StaticResource PhoneTextSubtleStyle}"/>

                        </StackPanel>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

你的 page.cs ..

 public partial class MainPage : PhoneApplicationPage
{

    public ObservableCollection<string> ListOFText { get; set; }

    public MainPage()
    {
        InitializeComponent();
        ListOFText = new ObservableCollection<string>();
        this.DataContext = this;


    }

    private void Mubutton_Click_1(object sender, RoutedEventArgs e)
    {


        Button btnObj = sender as Button;
        ListOFText.Add(btnObj.Content.ToString());

    }


}

希望这对您有所帮助..任何查询..下面的评论..

于 2013-07-17T10:08:28.133 回答