Reputation: 493
It sounds quite simple, but actually took me several hours and still not done. Here is my C# code:
public ItemEditor() {
InitializeComponent();
ListView_Item.ItemsSource = TestList;
}
List<Item> TestList = new List<Item>();
private void MenuItem_AddNewItem_Click(object sender, RoutedEventArgs e) {
Item newItem = new Item();
TestList.Add(newItem);
newItem.Name = "new item";
}
When function MenuItem_AddNewItem_Click is executed, the listview never update. I cannot figure out how can it be like this. And here is my xaml code:
<Grid DockPanel.Dock="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="200"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition Width="200"></ColumnDefinition>
</Grid.ColumnDefinitions>
<ListView x:Name="ListView_Item" Grid.Column="0" ContextMenu="{StaticResource ContextMenu_ListView_Item}" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<GridSplitter Grid.Column="0" Width="5"/>
<StackPanel Grid.Column="1" Orientation="Vertical">
<DockPanel>
<TextBlock>Name:</TextBlock>
<TextBox></TextBox>
</DockPanel>
</StackPanel>
<GridSplitter Grid.Column="1" Width="5"/>
<Grid Grid.Column="2"></Grid>
</Grid>
Can any one help me? I have looked at many webpages, there were similar situations, but non of them can help me solve my problem.
Upvotes: 1
Views: 64
Reputation: 126
As mentioned in the comments to the question, using an ObservableCollection is the simplest way to achieve this. It's what that collection is designed for. If you ever need to modify an existing custom collection type rather than using ObservableCollection you'd want to implement INotifyPropertyChanged and INotifyCollectionChanged.
Upvotes: 1