2010-10-10 68 views
2

这是我的代码:用户界面不会getUpdated

<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

您UI将为号集合内的变化进行更新,而不是在一个完全新收集完成。

要么扩展您的datacontext类以支持INotifyPropertyChanged(特别是数字)或不要重新创建数字。

+0

搞定了! tahnks! – Erez 2010-10-10 12:53:17

0

可以使集合的DependencyProperty这样(未测试):

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); } 
} 

然后更新参考集合将改变UI。