WPF How to get an object bound to a ListBoxItem element.

this is what i would like to do. I get a list of objects from the database and bind this list to the ListBox control. ListBoxItems elements consist of a text field and a button. Here is what I came up with. Until this moment, he works as intended. An object has several properties, such as ID, Name. If I click a button in a ListBoxItem element, the element must be removed from the ListBox, as well as from the database ...

<ListBox x:Name="taglistBox"> <ListBox.ItemContainerStyle> <Style TargetType="{x:Type ListBoxItem}"> <Setter Property="HorizontalAlignment" Value="Stretch"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="ListBoxItem"> <ContentPresenter HorizontalAlignment="Stretch"/> </ControlTemplate> </Setter.Value> </Setter> <Setter Property="Tag" Value="{Binding TagSelf}"></Setter> </Style> </ListBox.ItemContainerStyle> <ListBox.ItemTemplate> <DataTemplate> <Grid HorizontalAlignment="Stretch"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition/> </Grid.ColumnDefinitions> <Button Grid.Column="0" Name="btTag" VerticalAlignment="Center" Click="btTag_Click" HorizontalAlignment="Left"> <Image Width="16" Height="16" Source="/WpfApplication1;component/Resources/104.png"/> </Button> <TextBlock Name="tbtagBoxTagItem" Margin="5" Grid.Column="1" Text="{Binding Name}" VerticalAlignment="Center" /> </Grid> </DataTemplate> </ListBox.ItemTemplate> </ListBox> 

Textblock.Text is bound to the .Name object and ListBoxItem.Tag for object.TagSelf (which is only a copy of the object itself).

Now my questions

  • If I click the button in the listboxItem element, how do I get the listboxitem and its associated object. To remove an object from the database, I need to restore it somehow. I tried something like

    ListBoxItem lbi1 =
    (ListBoxItem) (taglistBox.ItemContainerGenerator.ContainerFromItem (taglistBox.Items.CurrentItem)); ObjectInQuestion t = (ObjectInQuestion) lbi1.Tag;

  • Is there a way to automatically update the contents of a ListBox when an Item Item changes? Right now i am achieving this

    taglistBox.ItemsSource = null;
    taglistBox.ItemsSource = ObjectInQuestion;

I would be grateful for any help you can give: D Thanks in advance

+5
c # data-binding wpf listbox listboxitem
source share
3 answers

Each ListBoxItem will have a corresponding item in the data collection bound to a DataContext. And each control in the ListBoxItem.ItemTemplate element inherits a DataContext. To get the object behind the pressed button, you can do the following in the click event handler:

 MyClass item = (MyClass)(sender as Button).DataContext; 

So that changes to the data source are automatically updated in the list, you can use ObservableCollection.

+35
source share

Use ObservableCollection to view instant automatic changes.

Get SelectedItem Using a DataContext

 private void Button_Click(object sender, RoutedEventArgs e) { var btn = sender as Button; myListBox.SelectedItem = btn.DataContext; } 

You must have the SelectedItem property in your view model bound to the listbox selectedItem element. Remove this specific item from your ObservableCollection. Your list will reflect the changes.

+1
source share

To question 2: Use an ObservableCollection. This implements INotifyCollectionChanged, so items will be added and removed as the collection changes.

Question 1: Send the sender as a button and use its DataContext. This will be the element to which it is attached. If you also need to get the ListBoxItem itself, you can VisualTreeHelper.GetParent() search the tree.

+1
source share

All Articles