繁体   English   中英

Windows Phone 8应用中使用ListBox的数据绑定

[英]Data binding in Windows Phone 8 app using ListBox

这是我的代码。 在做什么 我有一个带有文本书名的文本框(按钮),当我单击它时,可以使用绑定更改文本框中的文本。 但是现在我为作者名称添加了另一个文本框,但我不知道如何绑定它。 如果我使用与书名相同的方法,则它不起作用,或者书作者中书名中的文本也是如此。 所有文本都通过弹出设置页面进行更改。

我的资料来源: https : //dl.dropbox.com/u/40039421/App1.rar 图片在这里: https : //dl.dropbox.com/u/40039421/helpp.png

 public partial class MainPage : INotifyPropertyChanged
{
    private ObservableCollection<Book> _books = new ObservableCollection<Book>();
    public ObservableCollection<Book> AllBooks
    {
        get { return _books; }
        set { _books = value; }
    }

    ...

    private void InitializeData()
    {

        var bookName1 = new Book { Id = 1, BookName = "Small red apple",AuthorName = "Daniel"};
        ...

        AllBooks.Add(bookName1);
       ...

        OnPropertyChanged("AllBooks");
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(null, new PropertyChangedEventArgs(propertyName));
        }
    }

    private void btnGreenBook_Click(object sender, RoutedEventArgs e)
    {
        var button = sender as Button;
        if (button != null)
        {
            Popup popup = new Popup();
            popup.VerticalOffset = 30;
            PopupSettingPage control = new PopupSettingPage();
            popup.Child = control;
            popup.IsOpen = true;

            control.btnSave.Click += (s, args) =>
            {
                var id = Convert.ToInt32(button.Tag);
                var book = AllBooks.FirstOrDefault(sub => sub.Id == id);
                if (book != null)
                {
                    book.BookName = control.BookName;
                    book.OnPropertyChanged("BookName");

                }

                popup.IsOpen = false;
            };

           ...

哦,亲爱的,这是一个简单的错误:)

您忘记在PopupSettingsPage.xaml的Xaml中添加AuthorName

<TextBox x:Name="tbInputAuthorName" Text="{Binding AuthorName, Mode=TwoWay}" VerticalAlignment="Center"/>

然后在MainPage中执行此操作

book.BookName = control.BookName;
book.OnPropertyChanged("BookName");
book.AuthorName = control.AuthorName;
book.OnPropertyChanged("AuthorName");

根据您的评论的其他答案:

为此,您必须在列表框中删除第二个堆栈面板。 而是使用WrapPanel控件。 搜索如何为WindowsPhone使用WrapPanel。

然后,您必须找到某种方法将背景设置为红色或绿色。 祝好运

确定,我使用包装面板,但问题仍然存在。 我有两个全景项目(我在项目1和项目2中发布的代码仅在第二个全景页面上显示了相同的代码),问题仍然是,如果我将某些值保存到全景项目1中的一个项目中,那么该值在全景项目中也是2,我不知道怎么做。 逻辑是在全景图1项目中单击全景图1项目更改值,在全景图2项目上单击单击全景图2项目值。 它看起来像panoram项目1的ID与panoram项目2的ID相同。这是完整的源代码: https : //dl.dropbox.com/u/40039421/App1SecondEdit.rar

<Grid x:Name="LayoutRoot" Background="Transparent">
     <!--Panorama control-->
    <phone:Panorama>
        <!--Panorama item 1-->
        <phone:PanoramaItem Header="Test">
            <Grid x:Name="PanelPanoramaItem1" 
          Grid.Row="1"
          Margin="25,0,12,0">
                <ListBox ItemsSource="{Binding AllBooks}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <toolkit:WrapPanel Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                   <Button Width="140"
                            Height="140"
                            toolkit:TiltEffect.IsTiltEnabled="True" Margin="0,0,0,5" Click="Button_Click_1" Tag="{Binding Id}" >
                                            <Button.Template>
                                                <ControlTemplate>
                                                    <Grid Background="Chartreuse">
                                                        <TextBlock Foreground="Black" Text="{Binding BookName}" />
                                                    </Grid>
                                                </ControlTemplate>
                                            </Button.Template>
                                        </Button>

                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
            </Grid>
        </phone:PanoramaItem>
        <!-- END Panorama item 1 -->

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM