Windows phone listbox - Hold item event

2019-09-04 05:49发布

问题:

My app has a two ListBoxes. I need, when the user hold a item from my ListBox1, the same item should appear in ListBox2. My current code works, but I need select item (touch item) before hold item. I know that reason is i'm using SelectedItem, but how do for hold event?

My code:

    private void holdListAdd(object sender, System.Windows.Input.GestureEventArgs e)
    {

        if (List1.SelectedItem != null)
        {
            Fields fi = (Fields)this.List1.SelectedItem;
           // fi.Quantity = txtQuantity.Text;



            if (List2.Items.Contains(List1.SelectedItem))
            {
                MessageBox.Show("Esse item já foi adicionado!");

            }

            else
            {

                List2.Items.Add(fi);
                MessageBox.Show("Item Adicionado com sucesso!");
            }
        }

    }

My XAML:

<ListBox Name="List1" ItemsSource="{Binding Items}" Hold="holdListAdd">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                                <TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

 <ListBox Name="List2">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Margin="0,0,0,17" Width="432" Height="78">
                                <TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                                <TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>

回答1:

Instead of attaching hold event handler to ListBox, try to attach the event handler to ListBoxItem or a control within ListBox's DataTemplate, for example :

<ListBox Name="List1" ItemsSource="{Binding Items}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Hold="holdListAdd" Margin="0,0,0,17" Width="432" Height="78">
                <TextBlock Grid.Column="0" Text="{Binding FNome}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
                <TextBlock Grid.Column="1" Text="{Binding FEstado}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

Then you can get the item that trigger hold event from sender parameter :

private void holdListAdd(object sender, System.Windows.Input.GestureEventArgs e)
{
    var dc = ((FrameworkElement)sender).DataContext;
    Fields fi = (Fields)dc;
    ....
}