列表框绑定

时间:2012-06-02 16:10:56

标签: wpf windows-phone-7

您好我将数据绑定到Listbox有问题。简而言之......我想列出我所有的Skydrive文件。

我的XAML

 <TextBlock Height="35" HorizontalAlignment="Left" Margin="9,6,0,0" Name="infoTextBlock" Text="" VerticalAlignment="Top" Width="Auto" />
            <my:SignInButton Name="signInButton1" ClientId="<correct ClientId>" Scopes="wl.signin wl.basic wl.skydrive" Branding="Windows" TextType="SignIn" SessionChanged="signInButton1_SessionChanged" HorizontalAlignment="Left"  VerticalAlignment="Top" Margin="198,-6,0,0" />
            <StackPanel Height="578" HorizontalAlignment="Left" Margin="10,50,0,0" Name="StackContentPanel" VerticalAlignment="Top" Width="440">
                <ListBox Height="465" Name="FileList" Width="380" ItemsSource="{Binding Files}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <TextBlock Text="{Binding Name}"/>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </StackPanel>

我的班级和cs。

    namespace EReader.ViewModel
    {
        public class File
        {
            public File()
            {}

            private string name;
            public string Name
            {
                get { return this.name; }
                set { this.name = value; }
            }

        }
    }

    public class FilesManager
    {
        public ObservableCollection<string> Files;

        public FilesManager()
        {
            Files = new ObservableCollection<string>();
        }

    }


namespace EReader
{
    public partial class MainPage : PhoneApplicationPage
    {
        private LiveConnectClient client;
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void signInButton1_SessionChanged(object sender, LiveConnectSessionChangedEventArgs e)
        {

            if (e.Status == LiveConnectSessionStatus.Connected)
            {


                client = new LiveConnectClient(e.Session);
                infoTextBlock.Text = "Signed in.";
                client.GetCompleted +=
                    new EventHandler<LiveOperationCompletedEventArgs>(OnGetCompleted);
                client.GetAsync("/me/skydrive/files/");
            }
            else
            {
                infoTextBlock.Text = "Not signed in.";
                client = null;
            }
        }

        void OnGetCompleted(object sender, LiveOperationCompletedEventArgs e)
        {
            //Gdy uda nam się podłaczyc do konta skydrive
            if (e.Error == null)
            {
                signInButton1.Visibility = System.Windows.Visibility.Collapsed;

                #region Nazwa użytkownika
                string firstName = "";
                string lastName = "";
                if (e.Result.ContainsKey("first_name") ||
                    e.Result.ContainsKey("last_name"))
                {
                    if (e.Result.ContainsKey("first_name"))
                    {
                        if (e.Result["first_name"] != null)
                        {
                            firstName = e.Result["first_name"].ToString();
                        }
                    }
                    if (e.Result.ContainsKey("last_name"))
                    {
                        if (e.Result["last_name"] != null)
                        {
                            lastName = e.Result["last_name"].ToString();
                        }
                    }
                    infoTextBlock.Text =
                        "Hello, " + firstName +" "+ lastName + "!";
                }
                else
                {
                    infoTextBlock.Text = "Hello, signed-in user!";
                }
                #endregion

                #region Wszyskite pliki

                List<object> data = (List<object>)e.Result["data"];

                FilesManager fileManager = new FilesManager();

                foreach (IDictionary<string,object> items in data)
                {
                    File file = new File();

                    file.Name= items["name"].ToString();

                    fileManager.Files.Add(file.Name);
                }

                FileList.ItemsSource = fileManager.Files;
                #endregion
            }
            else
            {
                infoTextBlock.Text = "Error calling API: " +
                    e.Error.ToString();
            }
        }


    }

2 个答案:

答案 0 :(得分:2)

Files必须是属性,而不是字段。

此外,{Binding Name}必须是{Binding},因为字符串没有Name属性。

答案 1 :(得分:0)

这必须是公共财产:

 public ObservableCollection<string> Files;

应该是

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

然后您的绑定将起作用