c# - 使用 linq 在列表中查找值?

标签 c# linq windows-phone-8 longlistselector

大家好,我正在开发 Windows Phone 应用程序,当用户点击列表框中的特定项目时,我正在努力访问内部列表元素,我得到这样的输出

enter image description here

例如:假设用户点击 [0]index item 我想得到 [0]1, 1 480,[2]749,[3]270,当用户点击 1 时索引项我想要得到像 [0]1, 1 这样的值810,[2]1080,[3]271

界面

 <phone:LongListSelector Name="list_professions"  
                         LayoutMode="List"   
                         Tap="list_professions_Tap" 
                         Padding="5,15,5,15" 
                         IsGroupingEnabled="True">
  </phone:LongListSelector>

Json 类和变量

   private class RootObject
    {
        public string flag { get; set; }
        public string message { get; set; }
        public Result result { get; set; }
    }
    private class Result
    {
        public List<List<string>> Professions { get; set; }
    }

在列表框中显示数据

  void onResponse(object sender, DownloadStringCompletedEventArgs e)
    {
        try
        {
            onLoadingStope(sender, e);
            var rootObject = JsonConvert.DeserializeObject<RootObject>(e.Result);

            string flag = rootObject.flag;
            string msg = rootObject.message;
            if (flag.Equals("1"))
            {

                foreach (var temp in rootObject.result.Professions[0])
                {

                    profess.Add(new Result() { Professions = rootObject.result.Professions });
                    var flattenProfessions = rootObject.result.Professions.SelectMany(x => x).ToList();
                    list_professions.ItemsSource = rootObject.result.Professions;

                }
            }
            else
            {
                Console.WriteLine("Error message - " + msg);
                MessageBox.Show("Oops! response : " + msg);
            }
        }
        catch(Exception ex)
        {

        }


    }

和 List Tap Here 我在访问分组项目时遇到问题 如何在用户点击项目时访问分组项目

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


    }

最佳答案

您不应使用 LongListSelector 的 Tap 事件,而应使用 use a DataTemplateuse the Tap event of the elements其中。像这样:

<phone:PhoneApplicationPage.Resources>
    <DataTemplate x:Key="TileDataTemplate">
        <Grid Background="{StaticResource TransparentBrush}"
              Margin="0, 0, 0, 12" Height="60">
            <TextBlock Text="{Binding Name}" Margin="60, 10, 0, 0" FontSize="24" Height="60">
            </TextBlock>
            <Image x:Name="GetName" Tap="GetName_Tap" Grid.Column="0" Source="/Assets/AppBar/Delete.png" Height="40" Width="40"
                            Margin="0, 6, 0, 5" HorizontalAlignment="Right" VerticalAlignment="Top" />
        </Grid>
    </DataTemplate>
</phone:PhoneApplicationPage.Resources>

注意 Image 上的 Tap

<phone:LongListSelector
                    SelectionChanged="MainLongListSelector_SelectionChanged"
                    Margin="10,6,0,0"
                    ItemsSource="{Binding Staff.Items}"
                    LayoutMode="Grid"
                    GridCellSize="400,80"
                    ItemTemplate="{StaticResource TileDataTemplate}"
                    />

获取事件的 DataContext 的方法如下:

private void GetName_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
   var element = (FrameworkElement)sender;
   StaffData data = (StaffData)element.DataContext;
   MessageBox.Show(data.Name);
}

关于c# - 使用 linq 在列表中查找值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30477448/

相关文章:

c# - 文件存在于 WPF 中

c# - Gridview 显示空行

c# - LINQ:将单个列表分离为多个列表

c# - 如何在非 UI 代码 Windows Phone 8 中获取 Dispatcher

c# - TabControl 选项卡标题在更改字体时会调整大小

c# - EF6 - 使用基类属性的派生类中的 TPH 外键映射

c# - 如何检查 HTML 元素是否包含文本内容

c# - 基于当前登录的用户使用 LINQ 处理不同的数据库?

c# - Windows Phone 8 应用程序出现 "You need to install an app for this task"错误

c# - 如何在不创建 List<string> 的情况下将图像源绑定(bind)到路径列表?