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