2012-02-12 40 views
0

我有一个与列表框位于同一屏幕上的文本框。当我从列表框中移除一个绑定到可观察集合的项目时,文本框会获得焦点。有没有办法阻止这种情况发生?从可观察集合中移除项目导致文本框集中

我正在使用MVVM,但是如果它修复了这个问题,我打算在代码背后放置一些代码。

编辑:

代码隐藏检视:

命名空间Offload.WinPhone.Views { 使用System.Windows.Controls; using 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

你能告诉我们一些代码吗?通常这不应该发生... – 2012-02-12 17:12:58

+0

我同意Rico。我有很多页面使用列表框,文本框和项目删除。从来没有见过这种情况发生,没有真正触发它的事情 – 2012-02-12 17:55:53

+0

使用调试器添加代码我发现它只发生在我到达此行时:quickNotes.Remove(selectedQuickNote); – deanvmc 2012-02-12 18:05:29

回答

1

我有同样的问题,一个可能的解决方案是设置TextBox为禁用修改您的收藏之前和之后再次启用它:

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

我不会称之为一个干净的方法,但它为我工作:)。

+0

尽管如此,我实际上只是将文本框放到了一个用户控件中,但它停止了它。 – deanvmc 2012-05-12 17:41:28