将第一个ListView选择的项绑定到第二个ListView

时间:2016-04-18 11:49:40

标签: c# sql wpf xaml listview

我有第一个和第二个ListView,如图所示:

enter image description here

我使用此代码填充了第一个ListView

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

} 

这是我的Xaml代码

<ListView x:Name="ListBox_Category_Names" VerticalAlignment="Stretch"  HorizontalAlignment="Left"
                                        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"
                                        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的代码

ListBox_Category_Names.ItemsSource = CategoriesList.get_CategoryList();

不是MVVM

现在我卡住了因为我需要将First ListView Selected Items绑定到第二个ListView。拜托,我真的需要帮助,过去三(3)周一直坚持这一点。提前谢谢。

1 个答案:

答案 0 :(得分:0)

你可以用两种方式做到:1)代码背后2)mvvm

第一种方式:处理后面代码中的按钮点击:

public void Button_Click(object sender, EventArguments arg)
{
  List<ListViewItem> mySelectedItems = new List<ListViewItem>();

  foreach(ListViewItem item in myListView.SelectedItems)
  {
    mySelectedItems.Add(item);
  }
}

第二种方式以mvvm的模式绑定多个选择,以便查看:

Part 1

Part 2

Part 3