2010-10-10 72 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。