[英]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.