将第一个listview选择的项绑定到第二个listview [英] Bind first listview slected items to second listview
问题描述
我有第一个和第二个ListView,如图所示:示例
我填充了第一个ListView,使用此代码
这是我的Xaml代码
I have first and second ListView, as shown in the image: Example
I populated the first ListView, using this code
This is my Xaml Code
<listview x:name="ListBox_Category_Names" verticalalignment="Stretch" horizontalalignment="Left" xmlns:x="#unknown" Width="auto" Height="300" SelectionMode="Single" Grid.Column="0" Margin="0,0,10,0" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="ListBox_Category_Names_SelectionChanged">
<listview.view>
<gridview>
<gridviewcolumn header="Category Name" displaymemberbinding="{Binding Category_Names}" width="280" />
<gridviewcolumn header="Amount " displaymemberbinding="{Binding Category_Amount, ConverterCulture=ig-NG, StringFormat=\{0:C\}}" width="130" />
</gridview>
</listview.view>
</listview>
<listview x:name="ListBox_Selected_Category" verticalalignment="Stretch" horizontalalignment="Stretch" xmlns:x="#unknown" Width="auto" Height="300" SelectionMode="Single" Grid.Column="1" Margin="10,0,0,0" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto" ItemsSource="{Binding SelectedItem, ElementName=ListBox_Category_Names}">
<listview.view>
<gridview>
<gridviewcolumn header="Category Name" displaymemberbinding="{Binding Category_Names}" width="250" />
<gridviewcolumn header="Amount">
<gridviewcolumn.celltemplate>
<datatemplate>
<textbox x:name="txtBox_amount" text="{Binding Category_Amount, ConverterCulture=ig-NG, StringFormat=\{0:C\}}" width="200" height="35" />
</datatemplate>
</gridviewcolumn.celltemplate>
</gridviewcolumn>
</gridview>
</listview.view>
</listview>
这是加载第一个ListBox的代码
This is the Code thats Loads the First ListBox
ListBox_Category_Names.ItemsSource = CategoriesList.get_CategoryList();
不是MVVM
现在我卡住了因为我需要将第一个ListView选定项绑定到第二个ListView。拜托,我真的需要帮助,过去三(3)周一直坚持这一点。在此先感谢。
我尝试过:
Not MVVM
Now I'm stuck cause I need to bind the First ListView Selected Items to the Second ListView. Please, I really need help, been stuck with this for the past three (3) weeks. Thanks in advance.
What I have tried:
class CategoriesList
{
public string Category_Names { get; set; }
public double Category_Amount { get; set; }
public static List<categorieslist> get_CategoryList()
{
try
{
SQLiteConnection con = new SQLiteConnection(" Data Source=system.sqlite; Version=3; Compress=True; ");
con.Open();
string query = " SELECT category_id, category_name, amount FROM acc_income_category WHERE deleted = 0 ORDER BY category_name ASC ";
SQLiteCommand cmd = new SQLiteCommand(query, con);
SQLiteDataReader dr = cmd.ExecuteReader();
var categories = new List<categorieslist>();
while (dr.Read())
{
CategoriesList cl = new CategoriesList();
cl.Category_Names = dr.GetString(1);
cl.Category_Amount = dr.GetDouble(2);
categories.Add(cl);
}
con.Close();
return categories;
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(ex.Message);
return null;
}
}
}
推荐答案
非常简单,只是参考以下代码,
Hi, Its very simple, just refer the following code,
List<CategoriesList> SelectedItemList = ListFirst.SelectedItems.Cast<CategoriesList>().ToList();
ListSecond.ItemsSource = SelectedItemList;
我的意思是, ListFirst 作为你的第一个ListView和 ListSecond 作为你的第二个ListView。
Here I meant, ListFirst as your first ListView and ListSecond as your second ListView.
这篇关于将第一个listview选择的项绑定到第二个listview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!