简体   繁体   English

当ObservableCollection中的元素的属性发生变化时更新ListBox项目

[英]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.

相关问题 ObservableCollection项目更改时如何更新ListBox? - How to update ListBox when ObservableCollection items changed? 使用ObservableCollection时更新列表框 - Update Listbox when using ObservableCollection 当BindingList中的现有项发生更改时,Listbox拒绝更新 - Listbox refuses to update when changes occur in existing items in a BindingList 基础ObservableCollection更改时,ComboBox项未更新 - ComboBox items not updating when underlying ObservableCollection changes 当某个 AvaloniaList 元素的字段发生变化时,如何使 ListBox 更新? - How make a ListBox update when the field of a certain AvaloniaList element changes? ObservableCollection中的Property更改时引发事件 - Raising event when Property in ObservableCollection changes 嵌套属性更改时,抛出ObservableCollection的CollectionChanged - Throw CollectionChanged of ObservableCollection when nested Property changes 在ObservableCollection中更改模型属性时更新UI? - Updating UI when a model property changes in an ObservableCollection? Observablecollection在更新属性时更新多个项目 - Observablecollection updates multiple items when updating a property ObservableCollection 内容发生变化时如何更新 FlexLayout - How to update FlexLayout when ObservableCollection content changes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM