简体   繁体   English

当使用多个表时,如何从XAML ListView获取ID?

[英]How do I get the id from a XAML ListView when more than one table is used?

I am populating a ListView with SQLite data from two or more tables. 我用来自两个或多个表的SQLite数据填充ListView When selecting from table I wish to get the ID of the selected item but lv.SelectedItem is returning a null value. 从表中选择时,我希望获取所选项目的ID,但是lv.SelectedItem返回null值。 No trouble with single tables. 单个表没有问题。

I have searched everywhere and most people are just having problems joining the tables and haven't got as far as selecting. 我到处搜索过,大多数人在加入表格时都遇到问题,还没有选择的余地。

Classes 班级

public int ShoppingListID
{
    get { return listID; }
    set { listID = value;}
}
public int ShopID
{
    get { return shopID; }
    set { shopID = value; }
}

public int ShopID
{            
    get { return shopID; }
    set { shopID = value; }
}

public string ShopName
{
    get { return shopName; }
    set { shopName = value; }
}

Join code (which works perfectly) 连接代码(效果很好)

using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{               
    var allList = (from s in conn.Table<ShoppingLists>() join p in 
                   conn.Table<Shops>() on s.ShopID equals 
                   p.ShopID select new
                   {
                       s.ShoppingListID,
                       p.ShopName
                   }).ToList();

    lvShoppingLists.ItemsSource = allList;
}

Bound to a Grid in XAML 绑定到XAML中的网格

<ListView x:Name="lvShoppingLists"             
          ItemSelected="LvShoppingLists_ItemSelected"> 
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <Grid>
                    <Label Grid.Column="0" 
                           TextColor="Black" 
                           Text="{Binding ShoppingListID}" />
                    <Label Grid.Column="1" 
                           TextColor="Black" 
                           Text="{Binding ShopName}" />
                </Grid>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Obtain the ID of the selected item 获取所选项目的ID

private void LvShoppingLists_ItemSelected(object sender,             
    SelectedItemChangedEventArgs e)
{ 
    var selectedList = lvShoppingLists.SelectedItem as ShoppingLists;
    GlobalVariables.SelectedShoppingListID = selectedList.ShoppingListID;
    Navigation.PushAsync(new ShoppingListPage());
}

Everything compiles and runs fine, but upon selecting an item from the ListView on the mainpage var selectedList returns a null . 一切都可以编译并正常运行,但是从ListView上的ListView选择一项时, var selectedList返回null Debug shows that the information ( id and Name ) is being carried to the selected item but not being converted. 调试显示信息( idName )被携带到所选项目,但没有被转换。 The null value means that the variable is unable to be used in the query for the new page and I get a database error. null值表示该变量无法在新页面的查询中使用,并且出现数据库错误。 I suspect that it is to do with the fact that the the class Shopping list has two int entries and the name is causing the problem. 我怀疑这与“购物”类具有两个int条目且名称引起问题这一事实有关。

Answer turns out to be:

Use a new class AllLists

 public int ShoppingListID
    {
        get { return listID; }
        set { listID = value; }
    }

    public string ShopName
    {
        get { return shopName; }
        set { shopName = value; }
    }

using (SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation))
{               
var allList = (from s in conn.Table<ShoppingLists>() join p in 
               conn.Table<Shops>() on s.ShopID equals 
               p.ShopID select new **AllLists**
               {
                   ShoppingListID = s.ShoppingListID,
                   ShopName = p.ShopName
               });

lvShoppingLists.ItemsSource = allList;
}

The selected item ID can then be 
var selectedList = lvShoppingLists.SelectedItem as AllLists;
GlobalVariables.SelectedShoppingListID = selectedList.ShoppingListID;
Navigation.PushAsync(new ShoppingListPage());

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

相关问题 如何在多个表格中检查ID值 - How do I check for an ID value in more than one table 如何从 XAML 中的 ListView 获取 selecteditem? - How do I get selecteditem from ListView in XAML? 如何将多个表绑定到列表视图 - How to databind more than 1 table to a listview 如何从主表中获取最大id,并基于此,在主表中插入一条记录,在子表中插入一个或多个条目 - how to get max id from master table and based on that insert one record in master table and one or more entry in child table 我什么时候在代码中需要多个TraceSource? - When do I need more than one TraceSource in code? 如何从XAML中CLR名称空间的类型而不是程序集(而不是声明类型)中映射类型? - How do I map a type from a CLR namespace in XAML from an assembly other than the one where it's declared? 如何从表中获取SELECT * FROM TABLE结果到多个变量中 - how to get SELECT * FROM TABLE results into more than one variable in c# 如何让我的计算器进行多次计算? (C#) - How can I get my calculator to do more than one calculation? (C#) 如何从第一个表获取ID来记录第二个表? - How do I get the ID from the first table to record the second? 当每种房型有多种时,如何使用LINQ找到可用的房间? - How do I find available rooms using LINQ, when there are more than one of each room type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM