0

So I have a listbox with a template in which I bind an ObservableCollection of objects called "TotalDebits".

I have two way to delete the items:

  • One by one through a context menu
  • By range through a delete button and by fetching the checked objects.

This works:

private void Delete_Click(object sender, RoutedEventArgs e)
{
    DeleteDebit((DirectDebit)(((MenuItem)sender).DataContext));
}

private void DeleteDebit(DirectDebit ddb)
{
    TotalDebits.Remove(ddb);
}

private void delete_Click(object sender, EventArgs e)
{
    DeleteDebitList();
}

private void DeleteDebitList()
{
    try
    {
        foreach (var ddb in TotalDebits.ToList())
            if (ddb.IsChecked)
               TotalDebits.Remove(ddb);

    }
    catch
    {

    }
 }

In both case, the items are properly deleted, the problem is, in the second case, the keyboard is showing up right after the items are deleted, for absolutely no reason.. Of course I can hide it right after by focusing on the list but it looks ugly and I wish I could find a way to prevent this issue from happening.

Edit:

You'll find hereunder the xaml of the page:

<controls:PivotItem Header="Direct Debit" >
            <Grid>
                <Grid.Resources>
                    <Storyboard x:Name="ListboxSizeIncrease">
                        <DoubleAnimation Storyboard.TargetName="DebitList" Storyboard.TargetProperty="Height"
                            From="475" To="380" Duration="0:0:.5"/>
                    </Storyboard>
                    <Storyboard x:Name="ListboxSizeDecrease">
                        <DoubleAnimation Storyboard.TargetName="DebitList" Storyboard.TargetProperty="Height"
                            From="380" To="475" Duration="0:0:.5"/>
                    </Storyboard>
                </Grid.Resources>

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

                <ListBox Grid.Row="0" x:Name="DebitList" Height="475" Margin="10,5,10,0" Tap="ListBox_Tap" ItemsSource="{Binding TotalDebits}" VerticalAlignment="Top" ItemContainerStyle="{StaticResource CustomListBoxItem}">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,2,0,2">
                                <Grid Margin="5,5,5,5" HorizontalAlignment="Stretch">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="60" />
                                        <ColumnDefinition Width="*"/>
                                        <ColumnDefinition Width="Auto"/>
                                    </Grid.ColumnDefinitions>
                                    <CheckBox x:Name="CheckItem" Grid.Column="0" IsChecked="{Binding IsChecked}" Checked="CheckItem_Checked" Unchecked="CheckItem_Unchecked" />
                                    <TextBlock x:Name="DescriptionBlock" FontSize="24" Grid.Column ="1" Text="{Binding Description}" HorizontalAlignment="Left" VerticalAlignment="Center">   
                                    </TextBlock>
                                    <TextBlock FontSize="24" Grid.Column ="2" Text="{Binding Amount}" TextAlignment="Right"  HorizontalAlignment="Right" VerticalAlignment="Center"/>
                                </Grid>
                                <toolkit:ContextMenuService.ContextMenu>
                                    <toolkit:ContextMenu Name="ContextMenu" IsZoomEnabled="False">
                                        <toolkit:MenuItem Name="Edit" Header="Edit" Click="Edit_Click"/>
                                        <toolkit:MenuItem Name="Delete"  Header="Delete" Click="Delete_Click"/>
                                    </toolkit:ContextMenu>
                                </toolkit:ContextMenuService.ContextMenu>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <Border Margin="12,5,12,5" Grid.Row="1" Background="LightGray"/>
                <Grid Grid.Row="1" Margin="14,8,14,8">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <TextBlock Grid.Column="0" Text="TOTAL DIRECT DEBIT" FontSize="28" FontWeight="Bold" Foreground="DarkSlateGray"/>
                    <TextBlock Grid.Column="1" x:Name="TotalBlock" Text="{Binding TotalValue}" FontSize="28" FontWeight="Bold" Foreground="DarkSlateGray"/>
                </Grid>


                <Grid Grid.Row="2">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>

                    <Border Margin="12,5,12,5" Background="LightGray">
                    <Grid Grid.Row="0">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="300"/>
                            <ColumnDefinition Width="*"/>
                        </Grid.ColumnDefinitions>
                        <StackPanel Orientation="Vertical" Grid.Column="0">
                            <TextBlock Margin="10,0,0,0" Text="Description" FontSize="20" Foreground="DarkSlateGray"/>
                            <TextBox x:Name="DescriptionBox" FontSize="15" />
                        </StackPanel>
                        <Grid Grid.Column="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <StackPanel Orientation="Vertical" Grid.Column="0">
                                <TextBlock Margin="0,0,0,0" Text="Amount" Foreground="DarkSlateGray"/>
                                <TextBox x:Name="AmountBox" FontSize="15" />
                            </StackPanel>
                            <StackPanel Orientation="Vertical" Grid.Column="1">
                                <TextBlock Margin="10,0,0,0" Text="." Foreground="DarkSlateGray"/>
                                <TextBox x:Name="DecimalBox" Width="60" FontSize="15" MaxLength="2"/>
                            </StackPanel>
                        </Grid>
                    </Grid>
                    </Border>
                </Grid>

            </Grid>
        </controls:PivotItem>
Marc
  • 352
  • 2
  • 6
  • 19
  • 1
    Can you show your xaml markup of this page (not only listbox and its ItemTemplate)? There can be some routed events, one of which is focusing on some textbox or something else. – Oleg Koposov Jan 31 '14 at 16:31
  • I'm pretty sure there aren't any routed events.. The very fact that this happens only when deleting during an iteration and not when deleting a single element is very strange. – Marc Feb 01 '14 at 18:40

0 Answers0