views:

24

answers:

2

this is my code:

<Window x:Class="WpfApplication1.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="300">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="60*" />
        <RowDefinition Height="202*" />
    </Grid.RowDefinitions>
    <Button Click="Button_Click">Click Me</Button>
    <ListView ItemsSource="{Binding Numbers}" Grid.Row="1">
    </ListView>
</Grid>

 public partial class Window2 : Window {

  public int index =0;
  public ObservableCollection<int> Numbers { get; set; }

  public Window2() {

     Numbers = new ObservableCollection<int>() { index, index, index, index };
     InitializeComponent();
     DataContext = this;
  }

  private void Button_Click(object sender, RoutedEventArgs e) {
     Numbers = new ObservableCollection<int>() { index, index, index, index };
     index++;
  }

}

+1  A: 

You UI will be updated for changes inside the Numbers collection, but not when a completely new collection is made.

Either extend your datacontext class to support INotifyPropertyChanged (esp. for Numbers) or don't re-create Numbers.

Henk Holterman
got you!! tahnks!!
Erez
A: 

You can make the Numbers collection a DependencyProperty this way (not tested) :

public static readonly DependencyProperty NumbersProperty = DependencyProperty.Register("Numbers", typeof(ObservableCollection<int>), typeof(Window2));

public ObservableCollection<int> Numbers
{
    get{ return this.GetValue(NumbersProperty) as ObservableCollection<int>; }
    set{ this.SetValue(NumbersProperty, value); }
}

Then updating the reference to the collection will change the UI.

Serious