Skip to main content

Microsoft Silverlight

Answered Question Issue with dynamic Listbox with buttonsRSS Feed

(0)

someone outthere
someone ...

Member

Member

23 points

33 Posts

Issue with dynamic Listbox with buttons

I don't think I'm the first one with this question, but I couldn't find an answer to the earlier questions that can solve my problems.  o.O

What I am trying to do is have a dynamic list of video.  I used a class to contain the video name and URL, put them into a list, then bind the list to a ListBox.  In each ListBox item, it will show the video name, and also has a button that will play the video with the given URL.

The problems I am facing right now are:

1. When I have multiple items in list, not all buttons are working.

2. Even for the buttons that are working, it will only identify itself as the latest added item, rather than its corresponding item.

 

The xmal I have is:

    <StackPanel x:Name="LayoutRoot" Background="White">
        <TextBox x:Name="txtbox" Width="200" Text="initial" />
        <Button x:Name="addbtn1" Width="80" Height="20" Content="Add1" Click="addbtn1_Click" />
        <Button x:Name="addbtn2" Width="80" Height="20" Content="Add2" Click="addbtn2_Click" />
        <Button x:Name="rmvbtn" Width="80" Height="20" Content="Remove" Click="rmvbtn_Click" />
        <ListBox x:Name="mylist" Width="200" Height="200">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Height="25" Orientation="Horizontal">
                        <TextBox Width="80" Text="{Binding Name}" />
                        <Button Width="50" Height="20" Content="show" Click="Button_Click"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>

 

 And the underlying .cs file:

using System.Collections.ObjectModel;

    public partial class Page : UserControl
    {
        private ObservableCollection<video> videoList = new ObservableCollection<video>();
        private video tmpvideo = new video();

        public Page()
        {
            InitializeComponent();
            mylist.ItemsSource = videoList;
        }
        private void addbtn1_Click(object sender, RoutedEventArgs e)
        {
            tmpvideo.Name = "V1";
            tmpvideo.URL = "U1";
            videoList.Add(tmpvideo);
        }
        private void addbtn2_Click(object sender, RoutedEventArgs e)
        {
            tmpvideo.Name = "V2";
            tmpvideo.URL = "U2";
            videoList.Add(tmpvideo);
        }
        private void rmvbtn_Click(object sender, RoutedEventArgs e)
        {
            if (videoList.Any()) videoList.RemoveAt(0);
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            tmpvideo = b.DataContext as video;
            txtbox.Text = tmpvideo.URL;
            return;
        }
    }
    public class video
    {
        private string VName;
        private string VURL;
        public string Name
        {
            get { return VName; }
            set { VName = value; }
        }
        public string URL
        {
            get { return VURL; }
            set { VURL = value; }
        }
    }

 

In the compiled page, if I clicked "Add1" then "Add2", the "V1" button will not work.  Ideally it should show "U1" in the textbox at top when I clicked the corresponding button, similar with "V2".

Similarly, in the compiled page, if I clicked "Add1" "Add2", click "V2" button (so that the top show "U2"), then click "Add1", the 1st and 3rd button in list will not work, while if I click on "V2" button it will show "U1" on top.

Ideally if the button can actually identify its index in the list that would be even better.  I tried to implement in ItemControl rather than ListBox, but same issues occur.

Thanks!

xamlmammal
xamlmammal

Member

Member

160 points

31 Posts

Re: Issue with dynamic Listbox with buttons

 My recommendation to you is to create a usercontrol that takes your Video data object as a property. And inside that user control is the list of the name and the button. Then I would set the content of the listboxitem to your new user control. Or... throw each usercontrol into a stackpanel that resides in a scroll viewer. It sounds more complicated but it gives you the freedom and capability that you have requested.

---
http://www.coreysportfolio.com/
::Corey Miller::

sladapter
sladapter

All-Star

All-Star

17441 points

3,172 Posts

Answered Question

Re: Issue with dynamic Listbox with buttons

Your problem is you re-used same tmpvideo without create a new instance:

Another thing to remember when you put Button in a ListBox, make sure to set ClickMode = "Press". Otherwise you have to click the ListItem first, the button click event won't fire on your first click. 

Try the following code:

 <DataTemplate>
                    <StackPanel Height="25" Orientation="Horizontal" Background="Transparent">
                        <TextBox Width="80" Text="{Binding Name}" />
                        <Button Width="50" Height="20" Content="show" ClickMode="Press" Click="Button_Click"/>
                    </StackPanel>
 </DataTemplate>

 


 private ObservableCollection<video> videoList = new ObservableCollection<video>();
        video tmpvideo;
        public T()
        {
            InitializeComponent();            
            mylist.ItemsSource = videoList;
            //MyData d = new MyData();
            //d.Text = "TEST";
            //this.Text.DataContext = d;            

            //StyleObject style = new StyleObject { FontFamily = new FontFamily("Arial"), FontSize = 14 };
            //Binding b = new Binding("FontFamily");
            //b.Source = style;
            //this.Text.SetBinding(TextBox.FontFamilyProperty, b);

            //b = new Binding("FontSize");
            //b.Source = style;
            //this.Text.SetBinding(TextBox.FontSizeProperty, b);
        }

        private void addbtn1_Click(object sender, RoutedEventArgs e)
        {
            tmpvideo = new video();
            tmpvideo.Name = "V11";
            tmpvideo.URL = "U11";
            videoList.Add(tmpvideo);
        }
        private void addbtn2_Click(object sender, RoutedEventArgs e)
        {
            tmpvideo = new video();
            tmpvideo.Name = "V22";
            tmpvideo.URL = "U22";
            videoList.Add(tmpvideo);
        }
        private void rmvbtn_Click(object sender, RoutedEventArgs e)
        {
            if (videoList.Any()) videoList.RemoveAt(0);
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Button b = sender as Button;
            tmpvideo = b.DataContext as video;
            txtbox.Text = tmpvideo.URL;            
        }

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

someone outthere
someone ...

Member

Member

23 points

33 Posts

Re: Issue with dynamic Listbox with buttons

Sweeeeeeeeeeeet!!!  It works!!!  Thanks~  :)

Two more questions:

1. Now the button can recognize itself as the video object.  Is it possible to recognize its own position in the list too?  I guess I can locate it in the list by name with find index, but that would create problem when there are multiple objects.  (I am implementing a delete button to delete an item from the list.  It is more like a good-to-have function.  ^^")

2.  I assume the memory handling of SL is pretty good?  Here I would be using "new" infinite times.  Seems like I've never heard of "free (memory)" in C#...

sladapter
sladapter

All-Star

All-Star

17441 points

3,172 Posts

Re: Issue with dynamic Listbox with buttons

1)  private void Button_Click(object sender, RoutedEventArgs e)
        {            
            Button b = sender as Button;
            tmpvideo = b.DataContext as video;
            int index = videoList.IndexOf(tmpvideo);
            txtbox.Text = tmpvideo.URL;            
        }

2) This is not SL specific. It's the behavior of .Net code. You have to create a new object if you put them into a list. You can not just re-use the same object over and over again.

When you put them into a list, the object in the list is not a copy of your object, it is that object. You can still make changes to that object after you add it to the list and it will affect the data in the list (they are the same one). You can do the following test:

private void addbtn1_Click(object sender, RoutedEventArgs e)
        {

tmpvideo = new video();
            tmpvideo.Name = "V1";
            tmpvideo.URL = "U1";
            videoList.Add(tmpvideo); // put it to the list


            tmpvideo.Name = "V11"; // make changes
            tmpvideo.URL = "U11";

 }

 Click addbtn1, you will see the data you added is V11, not V1.

 

 

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

someone outthere
someone ...

Member

Member

23 points

33 Posts

Re: Issue with dynamic Listbox with buttons

I see I see...Thanks a lot...I guess my video list is about done...:)

I am just wondering in .NET do I need to free memory...I only formally learned C and C++ before...(malloc)...so was taught to use "free" and "delete" after "new"...^^"

sladapter
sladapter

All-Star

All-Star

17441 points

3,172 Posts

Re: Issue with dynamic Listbox with buttons

You need to do nothing. The Garbage Collector should take care of this. Once the object has no references,  it will eventually(it might take a while) be collected by GC.

sladapter
Software Engineer
Aprimo, Inc

Please remember to mark the replies as answers if they answered your question

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities