[英]Update ListBox Items when there are changes in property of element in ObservableCollection
I am a newbie with windows phone development. 我是Windows Phone开发的新手。 I have a class named 'State'.
我有一个名为“州”的班级。 It keeps the name and capital of the state.
它保留了州的名称和首都。 I have implemented INotifyPropertyChanged Interface.
我已经实现了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));
}
}
}
The ObservableCollection maintains my list of states. ObservableCollection维护我的状态列表。 I want to update the changes in one property of one element of the Collection and reflect the changes in ListBox.
我想更新Collection的一个元素的一项属性中的更改,并在ListBox中反映更改。 However the changes are not reflected.
但是,更改不会反映出来。 Here is my MainPage class.
这是我的MainPage类。 I change the property value on button click event.
我更改按钮单击事件的属性值。
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;
}
}
Here is my XAML code 这是我的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>
Is there anyway i can notify the listbox that it has to update. 无论如何,我可以通知列表框它必须更新。
Thanks A Lot!! 非常感谢!!
You bind ObservableCollection<State>
twice. 您绑定
ObservableCollection<State>
两次。 First, you create new object in MainPage
constructor. 首先,您在
MainPage
构造函数中创建新对象。 And this is enough. 这就足够了。 When you do it in XAML
ItemsSource="{Binding MyStatelist Mode=TwoWay}"
, new instance of ObservableCollection<State>
is created. 在XAML
ItemsSource="{Binding MyStatelist Mode=TwoWay}"
,将创建ObservableCollection<State>
新实例。 That's why MyStatelist[0].Name = "Abcd"
doesn't work because MyStatelist
is not set as the source of items anymore. 这就是为什么
MyStatelist[0].Name = "Abcd"
不起作用的原因,因为MyStatelist
不再设置为项目的来源。 Just remove the binding in XAML. 只需删除XAML中的绑定即可。
One more thing. 还有一件事。 You have to add
ItemTemplate
to your ListBox
and correctly bind properties of State
class. 您必须将
ItemTemplate
添加到ListBox
并正确绑定State
类的属性。 Here's my example that works: 这是我的有效示例:
<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>
You are not binding to the properties yet. 您尚未绑定到属性。 Use this instead:
使用此代替:
<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.