Page view counter
Handling multiple Buttons in variable number of ListBoxItems
Last post 08-11-2008 5:12 PM by virorum. 6 replies.
Sort Posts:
04-08-2008 10:44 AM
Handling multiple Buttons in variable number of ListBoxItems

Hi,

 I'm working on a file uploader. Each file is displayed in a ListBox, showing its filename/size/thumbnail, and a delete button for each file. The relevant part of the XAML:


            <ListBox x:Name="FilesList" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding Mode=OneWay}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" MinWidth="80"/>
                                <ColumnDefinition Width="Auto" MinWidth="140"/>
                                <ColumnDefinition Width="*" MinWidth="200"/>
                                <ColumnDefinition Width="Auto" MinWidth="50"/>
                            </Grid.ColumnDefinitions>
                           
                            <Image Grid.Column="0" Source="{Binding Image}" MaxWidth="50" MaxHeight="50"/>
                            <TextBlock Style="{StaticResource GTB}" Grid.Column="1" Text="{Binding Name}"/>
                            <TextBlock Style="{StaticResource GTB}" Grid.Column="2" Text="{Binding Status}"/>
                            <Button Grid.Column="3" Content="Delete"/>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
 

The items are FileUploadItems, which have the Image, Name and Status bound to the relevant UI components.

 

        public Page()
        {
            InitializeComponent();

            this.Files = new ObservableCollection<FileUploadItem>();
            this.ContentGrid.DataContext = this.Files;
        }

 

Then the code that adds the items from a file dialog is:

    this.Files.Add(new FileUploadItem(fileInfo));

 

The problem I'm having is trying to:

a) Get any handler that works for every button press. If I put an event handler inside the XAML (e.g. Click="MyButtonHandler", with MyButtonHandler in Page.xaml.cs), it seems to only get called if I click the first button in the added items.

b) Actually remove the associated FileUploadItem from the Files list according to which button is pressed. When I've done MFC programming with data exchange etc. I've never really experienced this much trouble trying to programatically get to my data from the GUI components, but this has me stumped. I got as far as this:

        private void Button_Click(object sender, RoutedEventArgs e)
        {
           Button b = (Button)sender;          
            Grid g = (Grid)b.Parent;

            Panel h = (Panel)FilesList.ItemsHost;
            UIElementCollection c = h.Children;
            UIElement el = c[0];
            ListBoxItem item = (ListBoxItem)el;

       }

...trying to find some link between the two, but I feel I'm stumbling around in the dark with a relatively new technology that I personally am also new to! Any helpful hints or nudges in the right direction would be gratefully appreciated :)

Cheers,

Dave
 

obanite

Loading...
Joined on 03-27-2008
Posts 5
04-08-2008 7:37 PM
Re: Handling multiple Buttons in variable number of ListBoxItems

Dave,

For some background, have a look at this post of mine: http://silverlight.net/forums/p/12094/39359.aspx#39359

Considering your specific scenario, I think I'd recommend not using ItemsSource, but rather creating your own ListBoxItem+content and inserting it via the Items collection. That way you can manually hook up the Button event handlers and also track any additional data you might need (if necessary). While ItemsHost is an option for Beta 1, I believe it's being made private in a future release (for consistency with WPF), so I wouldn't depend to heavily on it.

Hope this helps!


http://blogs.msdn.com/Delay

This posting is provided "AS IS" with no warranties, and confers no rights.

David Anson

Loading...
Joined on 04-11-2006
Microsoft
Posts 70
04-09-2008 6:47 AM
Re: Handling multiple Buttons in variable number of ListBoxItems

Hi,

I'm also working on a multi file uploader (using silverlight 2.0 beta 1), and I came across a similar problem concerning listboxes and buttons.

I made a custom UserControl representing each file to be transferred, containing a "Stop/Remove" button each. The Listbox is dynamically populated with instances of the custom control, and AddHandler is used to handle the "Stop/Remove" button clicks (the UserControl raises a custom event to notify it's parent control, whilst the ListBox is inside the parent control).

What happened was: only the button on the topmost (within the ListBox) UserControl would work.

So, I therefore added a breakpoint at the event handler procedure inside of the user control (there's a "native" Button_Click event there, which contains a RaiseEvent statement to notify the parent). The breakpoint would never hit, except for the Button of the topmost UserControl.

I think this might be a bug with the ListBox: I rewrote my code using a StackPanel inside of a ScrollView, and this works as expected (all the buttons inside of all UserControl's raise their "native" click event with this configuration).

Greetings,

Chris B.

chris b

Loading...
Joined on 04-09-2008
Posts 3
04-09-2008 3:49 PM
Marked as Answer
Re: Handling multiple Buttons in variable number of ListBoxItems

chris b:

I think this might be a bug with the ListBox: I rewrote my code using a StackPanel inside of a ScrollView, and this works as expected (all the buttons inside of all UserControl's raise their "native" click event with this configuration).

If you're curious, try the same thing using ItemsControl instead of ListBox. I'm thinking the same problematic behavior will be present - which would suggest the bug is in ItemsControl instead. There are a couple of known issues with things inside an ItemsControl and I can bring this to the attention of the relevant folks if you confirm it's an ItemsControl issue.


http://blogs.msdn.com/Delay

This posting is provided "AS IS" with no warranties, and confers no rights.

David Anson

Loading...
Joined on 04-11-2006
Microsoft
Posts 70
05-08-2008 11:04 AM
Re: Handling multiple Buttons in variable number of ListBoxItems

This may be of help to you.

 Responding To Events From DataTemplate Controls in WPF

http://anoriginalidea.wordpress.com/2007/05/23/responding-to-events-from-datatemplate-controls-in-wpf/

PresstranAdam

Loading...
Joined on 05-08-2008
Posts 1
05-13-2008 5:27 PM
Re: Handling multiple Buttons in variable number of ListBoxItems

Here's a blog post I did specifically to help with this scenario:

http://blogs.msdn.com/delay/archive/2008/04/21/buttons-in-a-listbox-and-more-demonstration-of-some-useful-silverlight-techniques.aspx


http://blogs.msdn.com/Delay

This posting is provided "AS IS" with no warranties, and confers no rights.

David Anson

Loading...
Joined on 04-11-2006
Microsoft
Posts 70
08-11-2008 5:12 PM
Re: Handling multiple Buttons in variable number of ListBoxItems

David Anson:

That blog post is an absolute life-saver! I'd been tearing my hair our trying to get a Button in a listbox to respond without first having to select the listboxitem itself.

Phew Big Smile

Virorurm Media

virorum

Loading...
Joined on 07-08-2008
United Kingdom
Posts 21
Microsoft Communities