Silverlight:更新列表框模板项

时间:2009-05-03 10:08:57

标签: c# silverlight-2.0

我有列表框,点击事件我打开新面板,我更改了列表框的数据,更准确地说是图像源。我有问题如何更新列表框以获得新图片。提前致谢。 这是我的代码:

<ListBox x:Name="lbNarudzbe" MouseLeftButtonUp="lbNarudzbe_MouseLeftButtonUp" HorizontalAlignment="Center" MaxHeight="600">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <Image Margin="0,5,0,0" Width="50" Height="50" HorizontalAlignment="Center" Source="{Binding Path=Picture}" />
                                <TextBlock HorizontalAlignment="Center" FontSize="23" Text="{Binding Path=UkupnaCijena}" Width="80"/>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>




public partial class Page : UserControl
    {
        ObservableCollection<Narudzba> narudzbe = new ObservableCollection<Narudzba>();

        public Page()
        {
            InitializeComponent();

            narudzbe.Add(new Narudzba());
            narudzbe.Add(new Narudzba());
            narudzbe.Add(new Narudzba());
            narudzbe.Add(new Narudzba());

            lbNarudzbe.ItemsSource = narudzbe;

        }





     public class Narudzba
            {
               //...
                public string Picture
                {
                    get { return "picture source"; }
                }.....

2 个答案:

答案 0 :(得分:1)


基本上,当您想要更新列表框中的图片时,您正在更新Narudzba类的Picture属性,并且由于您的Narudzba类未实现INotifyPropertyChanged接口,因此列表框无法更新图片。

以下是一些可能有用的代码。

    public class Narudzba : System.ComponentModel.INotifyPropertyChanged
{
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    void Notify(string propName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propName));
        }
    }

    string _picturesource;

    public string Picture
    {
        get { return _picturesource; }
        set 
        { 
            _picturesource = value;
            Notify("Picture");
        }
    }

    public Narudzba(string picturesource)
    {
        _picturesource = picturesource;
    }
  }
}

然后lbNarudzbe_MouseLeftButtonUp事件代码应如下所示

    private void lbNarudzbe_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        Narudzba nb  = (Narudzba)lbNarudzbe.SelectedItem;
        nb.Picture = "http://somedomain.com/images/newpicture.jpg";            
    }

HTH。

答案 1 :(得分:0)

虽然不确定,但是你不能对列表框之外的图像块对象及其中的图像块对象进行相同的绑定吗?

相关问题