2017-11-09 6 views
0

現在、リストビューを使って項目を設定しています。この場合ユーザー名。
オブジェクトを作成するためにjsonを逆シリアル化しています。オブジェクトとNAMEプロパティを取得し、リストビューに名前を設定します。 私はできる限りそれを作ろうとしました選択した項目を削除します
私が何をしてどのように行っても、いつもエラーが発生します。 明らかにこれは本当に簡単なので、私はこれをプロッパーの方法で行う方法を理解する助けが必要です。リストビューからSELECTED項目を正しく追加および削除するには

コードが非常に細いため、現在の状態(下のコードを参照)でエラーはありません。この時点では、リストビューにも値が設定されていません。私はそれが台無しになったdatabindingだと思う。

これは私のMainWindow.xamlに含まれています。

<Grid> 
     <ListView Name="theListview" HorizontalAlignment="Left" Height="236" Margin="10,10,0,0" VerticalAlignment="Top" Width="497"> 

      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}"/> 
       </GridView> 
      </ListView.View> 
     </ListView> 
     <Button Name="btnAdd" Click="btnAdd_Click" Content="Add" HorizontalAlignment="Left" Margin="61,276,0,0" VerticalAlignment="Top" Width="75"/> 
     <Button Name="btnRemove" Click="btnRemove_Click" Content="Remove" HorizontalAlignment="Left" Margin="389,276,0,0" VerticalAlignment="Top" Width="75"/> 
    </Grid> 

そして、これはこのような何かが(あなたがObservableCollectionからそれらを削除する必要があります)動作するはずです、私のCS

using Newtonsoft.Json; 
using System.Collections.Generic; 
using System.Collections.ObjectModel; 
using System.IO; 
using System.Windows; 

namespace lvProject 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     string userdata; 
     private ObservableCollection<Users> theUsers = new ObservableCollection<Users>(); 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void PopulateListView() 
     { 
      using (StreamReader sr = new StreamReader("ops.json")) 
      { 
       userdata = sr.ReadToEnd(); 
       List<Users> accounts = JsonConvert.DeserializeObject<List<Users>>(userdata); 
       foreach (var account in accounts) 
       { 
        theUsers.Add(new Users { Name = account.Name }); 
       } 
      } 
     } 

     private void btnAdd_Click(object sender, RoutedEventArgs e) 
     { 
      //I got the code done for this part, I wont include it because it will make this too long. 
     } 

     private void btnRemove_Click(object sender, RoutedEventArgs e) 
     { 
      //I want to remove the SELECTED item from the listview here. 
     } 
    } 
} 

JSON

[ 
    { 
    "uuid": "a7c1987f-022c-4310-bd32-21614e7e37b8", 
    "name": "Stefan", 
    "created": "2017-11-09 21:54:40 +0100", 
    "source": "Server", 
    "expires": "forever", 
    "reason": "Banned by an operator." 
    }, 
    { 
    "uuid": "98e99e7a-df48-4b8b-adc9-e65c32410247", 
    "name": "Demauen", 
    "created": "2017-11-09 20:44:35 +0100", 
    "source": "Server", 
    "expires": "forever", 
    "reason": "Banned by an operator." 
    }, 
    { 
    "uuid": "d229d707-27ae-472b-b2df-205713a7c0e0", 
    "name": "Kakor", 
    "created": "2017-11-09 21:56:02 +0100", 
    "source": "Server", 
    "expires": "forever", 
    "reason": "Banned by an operator." 
    }, 
    { 
    "uuid": "af4e2469-0988-4c42-97df-3301a167b658", 
    "name": "STEFANOS", 
    "created": "2017-11-09 22:26:14 +0100", 
    "source": "Server", 
    "expires": "forever", 
    "reason": "Banned by an operator." 
    }, 
    { 
    "uuid": "12e343aa-2c18-45ad-a485-b37838eecd3d", 
    "name": "charlies", 
    "created": "2017-11-09 22:26:22 +0100", 
    "source": "Server", 
    "expires": "forever", 
    "reason": "Banned by an operator." 
    }, 
    { 
    "uuid": "b87e1cbc-c67c-4026-a359-8652ad9de8b4", 
    "name": "iVarga", 
    "created": "2017-11-09 20:44:38 +0100", 
    "source": "Server", 
    "expires": "forever", 
    "reason": "Banned by an operator." 
    }, 
    { 
    "uuid": "eb04608e-cadc-4970-aa57-c60c43fce352", 
    "name": "steffe", 
    "created": "2017-11-09 22:09:14 +0100", 
    "source": "Server", 
    "expires": "forever", 
    "reason": "Banned by an operator." 
    }, 
    { 
    "uuid": "5f95fb35-a2aa-42ac-a99b-e88a013f51b9", 
    "name": "Alan", 
    "created": "2017-11-09 21:38:20 +0100", 
    "source": "Server", 
    "expires": "forever", 
    "reason": "Banned by an operator." 
    } 
] 

答えて

0

です:

private void btnRemove_Click(object sender, RoutedEventArgs e) 
{ 
    List<Users> usersToRemove = new List<Users>(); 

    foreach (Users item in theListview.SelectedItems) 
    { 
     usersToRemove.Add(item); 
    } 

    foreach (Users userToRemove in usersToRemove) 
    { 
     theUsers.Remove(userToRemove); 
    } 
} 
0

あなたは01を設定する必要がありますどこかのListViewObservableCollection

その後、UsersListViewSelectedItemをキャストし、ソースコレクションから削除することができます:

public partial class MainWindow : Window 
{ 
    string userdata; 
    private ObservableCollection<Users> theUsers = new ObservableCollection<Users>(); 

    public MainWindow() 
    { 
     InitializeComponent(); 
     theListview.ItemsSource = theListview; 
    } 

    private void PopulateListView() 
    { 
     using (StreamReader sr = new StreamReader("ops.json")) 
     { 
      userdata = sr.ReadToEnd(); 
      List<Users> accounts = JsonConvert.DeserializeObject<List<Users>>(userdata); 
      foreach (var account in accounts) 
      { 
       theUsers.Add(new Users { Name = account.Name }); 
      } 
     } 
    } 

    private void btnAdd_Click(object sender, RoutedEventArgs e) 
    { 
     //I got the code done for this part, I wont include it because it will make this too long. 
    } 

    private void btnRemove_Click(object sender, RoutedEventArgs e) 
    { 
     Users selectedUser = theListview.SelectedItem as Users; 
     if (selectedUser != null) 
      theUsers.Remove(selectedUser); 
    } 
} 
関連する問題