我有一个与列表框位于同一屏幕上的文本框。当我从列表框中删除一个绑定到可观察集合的项目时,文本框会获得焦点。有没有办法阻止这种情况发生?
我正在使用MVVM,但如果解决了这个问题,我可以在代码中加入一些代码。
编辑:
CodeBehind View:
命名空间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);
}
}
答案 0 :(得分:1)
我遇到了同样的问题,可能的解决方法是在修改集合之前将TextBox
设置为禁用,然后再次启用它:
TextBox.IsEnabled = false;
ObservableCollection.Remove(object);
TextBox.IsEnabled = true;
我不会称之为干净的方法,但它对我有用:)。