Silverlight:更新列表框模板项

发布于 2024-07-18 21:22:01 字数 1520 浏览 3 评论 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"; }
                }.....

I have listbox and on click event I open new panel where i change data of listbox, more accurately image source. I have problem how to update listbox to have new picture. Thanks in advance.
Here is my code:

<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"; }
                }.....

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪荡不羁 2024-07-25 21:22:01

基本上,当您想要更新列表框中的图片时,您正在更新 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 所示。


Basically when you want to update the picture in the listbox, you are updating the Picture property of your Narudzba class, and since your Narudzba class does not implement the INotifyPropertyChanged interface the listbox can't update the picture.

Here's some code that might help.

    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;
    }
  }
}

Then the lbNarudzbe_MouseLeftButtonUp event code should look like this

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

HTH.

谁人与我共长歌 2024-07-25 21:22:01

虽然不确定,但是列表框外部的图像块对象和列表框内部的图像块对象不能有相同的绑定吗?

Not sure though, but can you not have same binding for the imageblock object outside the listbox and the one inside it?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文