2014-04-21 2 views
1

필기 노트 앱을 개발하고, 메모 목록을 저장하기 위해 ItemsNote를 사용하고, 수정시 임시 아이템을 저장하기 위해 ItemModifyNote를 사용했습니다.WP8의 바인딩 아이템 문제

(나는 ItemsNote가 LongListSelector 내에서 바인딩 표시)에서 MainPage.xaml에서
public ObservableCollection<NoteViewModel> ItemsNote 
{ 
    get 
    { 
     return _itemsNote; 
    } 
    set 
    { 
     _itemsNote = value; 
     NotifyPropertyChanged("ItemsNote"); 
    } 
}   

public NoteViewModel ItemModifyNote { get; set; } 

, 나는, 각 음표 옆에 "편집"버튼을 삽입 내가 그것을 클릭하면 그래서, 나는 선택 항목으로 ItemModifyNote의 데이터를 설정 ItemsNote 후 나는이 텍스트 상자에 의해 (제목과 콘텐츠, 두 문자열입니다 포함) ItemModifyNote의 데이터를 수정하려면 modifyNotePage.xaml에서 "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)); 
    } 
} 

로 이동

<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에서 항목의 데이터를 설정 : 나는이 (가) 취소 버튼을 클릭하는 경우에도 메모가 여전히 수정 텍스트를 저장 ???

답변

0

ItemModifyNoteItemsNoteNoteViewModel 인스턴스를 참조하기 때문입니다. 편집 페이지의 TextBox과 기본 페이지의 LongListSelector이 동일한 veiwmodel 인스턴스에서 작동하므로 사용자가 ItemModifyNote 속성을 수정하면 LongListSelector은 더 이상의 코드가 필요하지 않고 업데이트 된 값을 표시합니다.

이 문제를 방지하려면 단추 편집 이벤트 처리기 메서드에서 NoteViewModel이라는 새 인스턴스를 만들고 기존 인스턴스를 직접 참조하는 대신 ItemsNote에있는 속성 값을 복사 해보십시오.

+0

음, 변경했습니다. App.ViewModel.ItemModifyNote = App.ViewModel.ItemsNote.ElementAt (intIndex); App.ViewModel.ItemModifyNote = new NoteViewModel() {NoteTitle = ..., NoteContent = ...}; , 그리고 이것은 효과가있는 것처럼 보입니다. 감사합니다 ^^ – user3448806

+0

그레이트 일을 가지고있어! 천만에요 :) – har07