WPF - 如何获取绑定到ListBoxItem的对象 [英] WPF - How do I get an object that is bound to a ListBoxItem back
问题描述
该对象具有多个属性,如ID,Name。如果我点击ListBoxItem中的按钮,项目应该从ListBox中删除,也可以从数据库中删除...
; ListBox x:Name =taglistBox>
< ListBox.ItemContainerStyle>
< Style TargetType ={x:Type ListBoxItem}>
< Setter Property =HorizontalAlignmentValue =Stretch/>
< Setter Property =Template>
< Setter.Value>
< ControlTemplate TargetType =ListBoxItem>
< ContentPresenter HorizontalAlignment =Stretch/>
< / ControlTemplate>
< /Setter.Value>
< / Setter>
< Setter Property =TagValue ={Binding TagSelf}>< / Setter>
< / Style>
< /ListBox.ItemContainerStyle>
< ListBox.ItemTemplate>
< DataTemplate>
< Grid HorizontalAlignment =Stretch>
< Grid.ColumnDefinitions>
< ColumnDefinition Width =Auto/>
< ColumnDefinition />
< /Grid.ColumnDefinitions>
< Button Grid.Column =0Name =btTagVerticalAlignment =CenterClick =btTag_ClickHorizontalAlignment =Left>
< Image Width =16Height =16Source =/ WpfApplication1; component / Resources / 104.png/>
< / Button>
< TextBlock Name =tbtagBoxTagItemMargin =5Grid.Column =1Text ={Binding Name}VerticalAlignment =Center/>
< / Grid>
< / DataTemplate>
< /ListBox.ItemTemplate>
< / ListBox>
Textblock.Text绑定到object.Name,ListBoxItem.Tag绑定到object.TagSelf(其中只是对象本身的副本)。
现在我的问题
-
如果我单击listboxItem中的按钮,我如何获取listboxitem和绑定到它的对象。为了从数据库中删除对象,我必须以某种方式检索它。我尝试过像
ListBoxItem lbi1 =
(ListBoxItem)(taglistBox.ItemContainerGenerator.ContainerFromItem(taglistBox.Items.CurrentItem));
ObjectInQuestion t =(ObjectInQuestion)lbi1.Tag; -
如果Itemssource更改,是否有方法自动更新ListBox的内容?现在我通过
taglistBox.ItemsSource = null;
taglistBox.ItemsSource = ObjectInQuestion;
感谢您提供的任何帮助:D
提前感谢
每个ListBoxItem都将数据绑定集合中的相应项目作为DataContext。而ListBoxItem.ItemTemplate中的每个控件都将继承DataContext。要获得点击按钮后面的对象,您可以在点击事件处理程序中执行以下操作:
MyClass item =(MyClass)发件人为按钮).DataContext;
要使数据源中的更改自动更新到列表框,可以使用ObservableCollection。 / p>
here is what I would like to do. I get a List of objects from a database and bind this list to a ListBox Control. The ListBoxItems consist of a textbox and a button. Here is what I came up with. Up to this point it works as intended. The object has a number of Properties like ID, Name. If I click on the button in the ListBoxItem the Item should be erased from the ListBox and also 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>
The Textblock.Text is bound to object.Name and the ListBoxItem.Tag to object.TagSelf (which is just a copy of the object itself).
Now my questions
If I click the button in the listboxItem how do I get the listboxitem and the object bound to it back. In order to delete the object from the database I have to retrieve 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 the ListBox if the Itemssource changes? Right now I'm achieving that by
taglistBox.ItemsSource = null;
taglistBox.ItemsSource = ObjectInQuestion;
I'd appreciate any help you can give :D Thanks in advance
Each ListBoxItem will have the corresponding item in the data bound collection as DataContext. And each control in the ListBoxItem.ItemTemplate will inherint the DataContext. To get the object behind the clicked button you can do the following in the click event handler:
MyClass item = (MyClass)(sender as Button).DataContext;
To have changes in the data source automatically updated to the list box, you can use an ObservableCollection.
这篇关于WPF - 如何获取绑定到ListBoxItem的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!