简体   繁体   English

如何处理LongListMultiSelector的选定项目?

[英]How do I handle a selected item of LongListMultiSelector?

I have list of items in LongListMultiSelector - how to handle a selected item? 我在LongListMultiSelector中有项目列表-如何处理选定的项目?

My LongListMultiSelector xaml: 我的LongListMultiSelector xaml:

<tkit:LongListMultiSelector Name="longlist" SelectionChanged="longlist_SelectionChanged">
    <tkit:LongListMultiSelector.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Title}" FontSize="32" Tap="TextBlock_Tap"/>
        </DataTemplate>
    </tkit:LongListMultiSelector.ItemTemplate>
</tkit:LongListMultiSelector>

TextBlock tap event handler code: TextBlock点击事件处理程序代码:

private void TextBlock_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
    var itemTapped = (sender as FrameworkElement).DataContext as Book;
}

LongListMultiSelector SelectionChanged event handler code: LongListMultiSelector SelectionChanged事件处理程序代码:

private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{

}

I found part of solution here , however, The problem if at least one item is selected, then textblockTap event doesn't handle - longlist_SelectionChanged event handles everything. 我在这里找到了解决方案的一部分,但是,如果选择了至少一项,那么问题就不解决了,那么textblockTap事件将无法处理-longlist_SelectionChanged事件将处理所有问题。 How can i fix that? 我该如何解决?

Once you are using LongListMultiSelector , the SelectionChanged event is fired when item is added or removed. 一旦使用LongListMultiSelector ,添加或删除项目时就会触发SelectionChanged事件。 If you want to perform the action regardless item is added/removed, I've managed to do it like this (for a simle string): 如果您想执行操作而不管添加/删除了项目,我已经设法做到这一点(对于字符串):

private void longlist_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    string selectedItem = String.Empty;
    if (e.AddedItems.Count > 0) selectedItem = e.AddedItems[0] as string;
    else selectedItem = e.RemovedItems[0] as string;
    MessageBox.Show(selectedItem); // do your work
}

It should run while items are selected separately by tapping, but this method will have problems when more items are added/removed at the same time - if you need it, then you should handle this also. 它可以在通过单击分别选择项目时运行,但是当同时添加/删除更多项目时,此方法会出现问题-如果需要,则也应进行处理。

Your XAML DataTemplate. 您的XAML DataTemplate。

    <DataTemplate x:Key="listItemTemplate">
        <StackPanel Orientation="Horizontal" Margin="4,4">
            <TextBlock Tap="textblockTap" Margin="0,-7,0,0" Text="{Binding Name}" Style="{StaticResource PhoneTextLargeStyle}"/>
        </StackPanel>
    </DataTemplate>

In your CS page; 在您的CS页面中;

    private void textblockTap(object sender, EventArgs e)
    {
            var file = (TextBlock)sender;
            var ContentFile = (string)file.Text;
            MessageBox.Show(ContentFile);
    }

This example will show you the text of the selected item in the MessageBox. 本示例将向您显示MessageBox中所选项目的文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM