繁体   English   中英

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM