![](/img/trans.png)
[英]How to update ListBox when ObservableCollection items changed?
[英]Update ListBox Items when there are changes in property of element in ObservableCollection
我是Windows Phone開發的新手。 我有一個名為“州”的班級。 它保留了州的名稱和首都。 我已經實現了INotifyPropertyChanged接口。
public class State:INotifyPropertyChanged
{
private string _name;
private string _capital;
public State() { }
public State(string name, string capital)
{
this.Name=name;
this.Capital=capital;
}
public string Name
{ get
{
return _name;
}
set
{
_name=value;
OnPropertyChanged(this,"Name");
OnPropertyChanged(this, "Name");
}
}
public string Capital
{
get
{
return _capital;
}
set
{
_capital = value;
OnPropertyChanged(this, "Capital");
OnPropertyChanged(this, "Name");
}
}
public override string ToString()
{
return (Name + " " + Capital );
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(object sender,string Property)
{
if (PropertyChanged != null)
{
PropertyChanged(sender, new PropertyChangedEventArgs(Property));
}
}
}
ObservableCollection維護我的狀態列表。 我想更新Collection的一個元素的一項屬性中的更改,並在ListBox中反映更改。 但是,更改不會反映出來。 這是我的MainPage類。 我更改按鈕單擊事件的屬性值。
public partial class MainPage : PhoneApplicationPage
{
private ObservableCollection<State> MyStatelist;
public MainPage()
{
InitializeComponent();
MyStatelist = new ObservableCollection<State>();
MyStatelist.Add(new State("State1","Capital1"));
States.ItemsSource = MyStatelist;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MyStatelist[0].Name = "Abcd";
t1.Text = MyStatelist[0].Name;
}
}
這是我的XAML代碼
<ListBox x:Name="States" Margin="0,10" ItemsSource="{Binding MyStatelist Mode=TwoWay}" ScrollViewer.VerticalScrollBarVisibility="Visible" Background="White" Foreground="Black" Height="Auto" Width="Auto" >
</ListBox>
無論如何,我可以通知列表框它必須更新。
非常感謝!!
您綁定ObservableCollection<State>
兩次。 首先,您在MainPage
構造函數中創建新對象。 這就足夠了。 在XAML ItemsSource="{Binding MyStatelist Mode=TwoWay}"
,將創建ObservableCollection<State>
新實例。 這就是為什么MyStatelist[0].Name = "Abcd"
不起作用的原因,因為MyStatelist
不再設置為項目的來源。 只需刪除XAML中的綁定即可。
還有一件事。 您必須將ItemTemplate
添加到ListBox
並正確綁定State
類的屬性。 這是我的有效示例:
<ListBox x:Name="States" Margin="0,10"
Grid.Row="1"
ScrollViewer.VerticalScrollBarVisibility="Visible"
Background="White"
Foreground="Black"
Height="Auto"
Width="Auto">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Name}" Margin="0, 0, 12, 0"/>
<TextBlock Text="{Binding Capital}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
您尚未綁定到屬性。 使用此代替:
<ListBox x:Name="States" Margin="0,10" ScrollViewer.VerticalScrollBarVisibility="Visible" Background="White" Foreground="Black" Height="Auto" Width="Auto" >
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} {1}">
<Binding Path="Name" />
<Binding Path="Capital" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.