2012-02-12 4 views
0

を集中するテキストボックスが発生します。観察可能なコレクションにバインドされているリストボックスからアイテムを削除すると、テキストボックスにフォーカスが移ります。これが起こるのを止める方法はありますか?削除する項目は、私は、リストボックスと同じ画面上にあるテキストボックスを持っている

私はMVVMを使用していますが、それは、この問題を修正する場合はコードの後ろにいくつかのコードを置くことに開いています。

EDIT:

分離コードビュー:

名前空間Offload.WinPhone.Views {使用System.Windows.Controls。 Microsoft.Phone.Controlsを使用している ;

public partial class MainPageView : PhoneApplicationPage 
{ 
    public MainPageView() 
    { 
     InitializeComponent(); 
    } 

    private void QuickNoteBodyEditor_TextChanged(object sender, TextChangedEventArgs e) 
    { 
     var senderAsTextbox = (TextBox)sender; 

     if (senderAsTextbox.Text.Length == 0) 
      this.Focus(); 
    } 
} 

}

閲覧:

<Grid x:Name="LayoutRoot" Background="Transparent" Margin="12,0,12,0"> 

    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="Auto"/> 
    </Grid.RowDefinitions> 

    <TextBlock Text="OFFLOAD" FontFamily="Segoe WP Bold" Grid.Row="0"/> 

    <ListBox x:Name="QuickNotes" Grid.Row="1" TabIndex="1"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Margin="0,5,0,5" Orientation="Vertical"> 
        <TextBlock Style="{StaticResource PhoneTextExtraLargeStyle}" TextWrapping="Wrap" Text="{Binding Body}"/> 
        <TextBlock Style="{StaticResource PhoneTextAccentStyle}" Text="{Binding Timestamp, StringFormat='{}{0:dd MMM HH:mm}'}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

    <TextBox x:Name="QuickNoteBodyEditor" AcceptsReturn="True" Grid.Row="2" InputScope="Chat" TextChanged="QuickNoteBodyEditor_TextChanged" IsTabStop="True" /> 

</Grid> 

<phone:PhoneApplicationPage.ApplicationBar> 
    <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True"> 

     <shell:ApplicationBar.Buttons> 
      <cal:AppBarButton IconUri="/AppFramework/Resources/Icons/Add.png" Text="Add" Message="AddQuickNote" /> 
      <cal:AppBarButton IconUri="/AppFramework/Resources/Icons/Delete.png" Text="Delete" Message="DeleteSelectedQuickNote" /> 
     </shell:ApplicationBar.Buttons> 

     <shell:ApplicationBar.MenuItems> 
      <cal:AppBarMenuItem Text="About" Message="NavigateToAddAccountView"/> 
      <cal:AppBarMenuItem Text="Review" Message="NavigateToAddAccountView"/> 
     </shell:ApplicationBar.MenuItems> 

    </shell:ApplicationBar> 
</phone:PhoneApplicationPage.ApplicationBar> 

ビューモデル:

public class MainPageViewModel : PropertyChangedBase 
{ 
    private ObservableCollection<QuickNote> quickNotes; 
    private string quickNoteBodyEditor; 
    private QuickNote selectedQuickNote; 

    // Databound Properties 

    public string QuickNoteBodyEditor 
    { 
     get { return quickNoteBodyEditor; } 
     set { quickNoteBodyEditor = value; NotifyOfPropertyChange(() => QuickNoteBodyEditor); NotifyOfPropertyChange(() => CanAddQuickNote);} 
    } 

    public QuickNote SelectedQuickNote 
    { 
     get { return selectedQuickNote; } 
     set { selectedQuickNote = value; NotifyOfPropertyChange(() => SelectedQuickNote); NotifyOfPropertyChange(() => CanDeleteSelectedQuickNote); } 
    } 

    public ObservableCollection<QuickNote> QuickNotes 
    { 
     get { return quickNotes; } 
     set { quickNotes = value; NotifyOfPropertyChange(() => QuickNotes); } 
    } 

    // Guard Clauses 

    public bool CanAddQuickNote 
    { 
     get { return !string.IsNullOrWhiteSpace(quickNoteBodyEditor); } 
    } 

    public bool CanDeleteSelectedQuickNote 
    { 
     get{ return selectedQuickNote == null ? false : true; } 
    } 

    // Constructors 

    public MainPageViewModel() 
    { 
     GetQuickNotesFromIsolatedStorage(); 
     WatchWhatsGotFocus.StartWatching(); 
    } 

    // Public Methods 

    public void AddQuickNote() 
    { 
     if (CanAddQuickNote) 
     { 
      quickNotes.Add(new QuickNote(quickNoteBodyEditor, DateTime.Now)); 
      AddQuickNotesToIsolatedStorage(); 

      quickNoteBodyEditor = string.Empty; 

      NotifyOfPropertyChange(() => QuickNoteBodyEditor); 
      NotifyOfPropertyChange(() => QuickNotes); 
     } 
    } 

    public void DeleteSelectedQuickNote() 
    { 
     if (CanDeleteSelectedQuickNote) 
     { 
      quickNotes.Remove(selectedQuickNote); 
      AddQuickNotesToIsolatedStorage(); 

      selectedQuickNote = null; 

      NotifyOfPropertyChange(() => SelectedQuickNote); 
      NotifyOfPropertyChange(() => QuickNotes); 
     } 
    } 

    private void GetQuickNotesFromIsolatedStorage() 
    { 
     quickNotes = IsolatedStorage.Get<ObservableCollection<QuickNote>>("QuickNoteList"); 
    } 

    private void AddQuickNotesToIsolatedStorage() 
    { 
     IsolatedStorage.Add("QuickNoteList", quickNotes); 
    } 
} 
+1

あなたは私たちにいくつかのコードを表示することができますか?通常、これは...私はプエルトリコに同意 –

+0

起こるべきではありません。私はリストボックス、テキストボックス、およびアイテムの削除の両方を使用してページをたくさん持っています。実際にそれを引き起こすものがなければ、これは起こりませんでした。 –

+0

追加コード、私はこの行を取得するときにのみ発生見つけるデバッガ使用:quickNotes.Remove(selectedQuickNoteを)。私はあなたが選択したノートに応じて、テキストボックスの内容を変更すると思う – deanvmc

答えて

1

私は、可能な解決策は、あなたのコレクションを変更する前に無効にTextBoxを設定し、その後、再び、それを可能にすることにある同じ問題を持っていた:

TextBox.IsEnabled = false; 
ObservableCollection.Remove(object); 
TextBox.IsEnabled = true; 

は、私はきれいなアプローチ、それを呼び出すことはありませんが、それは私のために働きました:)。

+0

これはうまくいきますが、私は実際にテキストボックスをusercontrolにプッシュして停止しました。 – deanvmc

関連する問題