2017-10-10 63 views
0

我有這個項目,我想弄清楚如何添加到列表視圖,然後能夠刪除它們。但我似乎不能得到itemsource綁定,除非我硬編碼。我如何在XAML中執行此操作?如何在WPF中正確設置我的itemsource以顯示我的ObservableCollection項目?

這增加了一個項目列表視圖

public partial class MainWindow : Window 
    { 
     private ObservableCollection<myItem> Item; 
     const string pattern = @"((.*)) (.*) left the game"; 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void btnAppend_Click(object sender, RoutedEventArgs e) 
     { 
      Item = new ObservableCollection<myItem>() { new myItem() { Username = "Prabhat" } }; 
      lvUsers.ItemsSource = Item; 
     } 
    } 

但是,如果刪除了硬編碼的ItemSource它不添加它,即使我想補充的ItemSource = {結合產品}到YM XAML

<Grid> 
     <ListView Name="lvUsers" ItemsSource="{Binding Item}" HorizontalAlignment="Left" Height="107" Margin="10,10,0,0" VerticalAlignment="Top" Width="497"> 
      <ListView.View> 
       <GridView> 
        <GridViewColumn Header="Name" Width="100" DisplayMemberBinding="{Binding Username}"/> 
       </GridView> 
      </ListView.View> 
     </ListView> 
     <TextBox Name="tbConent" HorizontalAlignment="Left" Height="78" Margin="10,122,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="497"/> 
     <Button Name="btnAppend" Click="btnAppend_Click" Content="Append" HorizontalAlignment="Left" Margin="213,220,0,0" VerticalAlignment="Top" Width="75"/> 

    </Grid> 

編輯

public class myItem 
    { 
     public string Username { get; set; } 
    } 

EDIT 2

MainWindow.xaml.cs

using System.Collections.ObjectModel; 
using System.Windows; 

namespace Listviewssssssssssssss 
{ 
    public partial class MainWindow : Window 
    { 
     public ObservableCollection<myItem> Item { get; private set; } 

     public MainWindow() 
     { 
      InitializeComponent(); 
     } 

     private void btnAppend_Click(object sender, RoutedEventArgs e) 
     { 
      Item = new ObservableCollection<myItem>() { new myItem() { Username = "Prabhat" } }; 
      lvUsers.Items.Refresh(); 
     } 

     private void btnRemove_Click(object sender, RoutedEventArgs e) 
     { 

     } 
    } 
} 
+0

可能的複製[C#從列表中填充列表視圖](https://stackoverflow.com/questions/45584249/c-sharp-populating-a-listview-from-a-list) – ASh

+0

[MVVM](https://msdn.microsoft .com/en-us/library/hh848246.aspx)在使用WPF,特別是XAML Bindi ngs –

回答

3

Item的公共屬性:

public ObservableCollection<myItem> Item { get; private set; } 

而且可以綁定到它:

<ListView Name="lvUsers" ItemsSource="{Binding Item, RelativeSource={RelativeSource AncestorType=Window}}" ...> 

製作確定myItem是一種公共類型:

public class myItem { ... } 

或者你可以設置窗口的DataContext本身:

public partial class MainWindow : Window 
{ 
    public ObservableCollection<myItem> Item { get; private set; } 
    const string pattern = @"((.*)) (.*) left the game"; 

    public MainWindow() 
    { 
     InitializeComponent(); 
     DataContext = this; 
     Item = new ObservableCollection<myItem>() { new myItem() { Username = "Prabhat" } }; 
    } 
} 

...並綁定到源集合直接:

<ListView Name="lvUsers" ItemsSource="{Binding Item}" ...> 

你也應該考慮可能重命名該物業爲「物品s」,因爲它是一個集合。

編輯:

編輯:如果你打算以後初始綁定動態設置Item屬性,還需要實現INotifyPropertyChanged接口,提高變更通知:的

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    private ObservableCollection<myItem> _items; 
    public ObservableCollection<myItem> Item 
    { 
     get { return _items; } 
     set { _items = value; NotifyPropertyChanged(); } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void btnAppend_Click(object sender, RoutedEventArgs e) 
    { 
     Item = new ObservableCollection<myItem>() { new myItem() { Username = "Prabhat" } }; 
    } 

    private void btnRemove_Click(object sender, RoutedEventArgs e) 
    { 

    } 

    public event PropertyChangedEventHandler PropertyChanged; 
    private void NotifyPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] String propertyName = "") 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 
+0

更改modofer給了我這種不一致的可訪問性:字段類型'ObservableCollection '比字段'MainWindow.Item' –

+0

更不易訪問myItem類也必須是公共的。看我的編輯。 – mm8

+0

啊廢話!對不起,沒有看到,謝謝:D –

相關問題