我開發了一個記錄應用程序,使用ItemsNote保存筆記列表,並使用ItemModifyNote在修改時保存臨時項目。WP8中綁定項的問題
public ObservableCollection<NoteViewModel> ItemsNote
{
get
{
return _itemsNote;
}
set
{
_itemsNote = value;
NotifyPropertyChanged("ItemsNote");
}
}
public NoteViewModel ItemModifyNote { get; set; }
在MainPage.xaml中(其中,我顯示ItemsNote一個LongListSelector內結合),我插入一個「編輯」按鈕旁邊的每一個音符,所以當我點擊它,我在ItemModifyNote的數據集選擇的項目ItemsNote,然後導航到「modifyNotePage.xaml」
private void btEditNote_Click(object sender, RoutedEventArgs e)
{
var button = (sender as Button).DataContext as NoteViewModel;
if (button != null)
{
int intIndex = App.ViewModel.ItemsNote.IndexOf(button);
string modifyUri = "/Pages/NoteModifyPage.xaml?Id=" + intIndex.ToString();
App.ViewModel.ItemModifyNote = App.ViewModel.ItemsNote.ElementAt(intIndex);
NavigationService.Navigate(new Uri(modifyUri, UriKind.RelativeOrAbsolute));
}
}
在ModifyNotePage.xaml,我通過2文本框修改ItemModifyNote的數據(包括標題和內容,兩者都是字符串)
<TextBox Grid.Column="1"
Text="{Binding ItemModifyNote.NoteTitle, Mode=TwoWay}" x:Name="tbxModifyNoteTitle"
FontFamily="Clear Sans Light" BorderThickness="0.0"
KeyDown="tbxModifyNoteTitle_KeyDown"/>
</Grid>
<TextBox Grid.Row="1" Margin="0,0,0,20"
x:Name="tbxModifyNoteContent" Text="{Binding ItemModifyNote.NoteContent, Mode=TwoWay}"
AcceptsReturn="True" TextWrapping="Wrap" BorderThickness="0.0" FontFamily="Clear Sans Light"
GotFocus="tbxModifyNoteContent_GotFocus" LostFocus="tbxModifyNoteContent_LostFocus"/>
終於我用了2個按鈕:取消並保存。
在保存按鈕我通過ItemModifyNote
private void btCancel_Click(object sender, EventArgs e)
{
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
}
private void btSave_Click(object sender, EventArgs e)
{
App.ViewModel.ItemsNote[key] = App.ViewModel.ItemModifyNote;
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
}
問題的數據集項目的ItemsNote數據:即使當我點擊取消按鈕,音符依然保存修改文字???
uhm,我已經改變了App.ViewModel.ItemModifyNote = App.ViewModel.ItemsNote.ElementAt(intIndex);與App.ViewModel.ItemModifyNote = new NoteViewModel(){NoteTitle = ...,NoteContent = ...}; ,這似乎工作。謝謝^^ – user3448806
太棒了,你有它的工作!別客氣 :) – har07