Click here to Skip to main content
15,890,527 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
XML
<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>

This is the Code thats Loads the First ListBox
C#
ListBox_Category_Names.ItemsSource = CategoriesList.get_CategoryList();

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:

C#
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;
        }
    }

}
Posted
Updated 22-Apr-16 0:37am
v2

1 solution

Hi, Its very simple, just refer the following code,
C#
List<CategoriesList> SelectedItemList = ListFirst.SelectedItems.Cast<CategoriesList>().ToList();
ListSecond.ItemsSource = SelectedItemList;

Here I meant, ListFirst as your first ListView and ListSecond as your second ListView.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900