Windows Phone列表框-保留项目事件 [英] Windows phone listbox - Hold item event

查看:89
本文介绍了Windows Phone列表框-保留项目事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有两个列表框.我需要,当用户按住我的ListBox1中的某个项目时,同一项目应出现在ListBox2中. 我当前的代码有效,但是我需要在保留项目之前选择项目(触摸项目).我知道原因是我正在使用SelectedItem,但是对于保持事件该怎么办?

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?

我的代码:

    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!");
            }
        }

    }

我的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>

推荐答案

而不是将保持事件处理程序附加到ListBox,请尝试将事件处理程序附加到ListBoxItemListBoxDataTemplate中的控件,例如:

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>

然后您可以从sender参数获取触发保持事件的项目:

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;
    ....
}

这篇关于Windows Phone列表框-保留项目事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆