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
c # data-binding wpf listbox listboxitem
Jonblumfeld
source share