2016-04-18 59 views
0

我具有第一和第二ListView的,如所示的圖像中:綁定首先的ListView slected項第二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_Category_Names.ItemsSource = CategoriesList.get_CategoryList(); 

不MVVM

現在我堅持,因爲我需要在第一ListView中選定的項目綁定到二ListView控件。請,我真的需要幫助,在過去的三(3)周裏一直堅持這一點。提前致謝。

+0

你可以發表你的'xaml'代碼呢? – SWilko

+0

如何分配第一個列表視圖的項目源?你在使用MVVM嗎? – Firoz

回答

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

+0

謝謝@Isreal祭壇,但我已經嘗試了第一個解決方案,但它給了我這個異常「附加信息:無法投入'School.CategoriesList'類型的對象來鍵入'System.Windows.Controls.ListViewItem'」。 –