簡體   English   中英

WPF - 如何獲取綁定到ListBoxItem的對象

[英]WPF - How do I get an object that is bound to a ListBoxItem back

這就是我想做的事情。 我從數據庫中獲取對象列表,並將此列表綁定到ListBox控件。 ListBoxItems包含一個文本框和一個按鈕。 這就是我想出的。 到目前為止,它按預期工作。 該對象有許多屬性,如ID,Name。 如果我單擊ListBoxItem中的按鈕,則應該從ListBox和數據庫中刪除Item ...

<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綁定到object.Name,ListBoxItem.Tag綁定到object.TagSelf(它只是對象本身的副本)。

現在我的問題

  1. 如果我單擊listboxItem中的按鈕,我如何獲取listboxitem以及綁定到它的對象。 為了從數據庫中刪除對象,我必須以某種方式檢索它。 我試過類似的東西

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

  2. 如果Itemssource更改,有沒有辦法自動更新ListBox的內容? 現在我正在實現這個目標

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

我很感激你能給予的任何幫助:D提前謝謝

每個ListBoxItem都將數據綁定集合中的相應項作為DataContext。 ListBoxItem.ItemTemplate中的每個控件都將繼承DataContext。 要獲取單擊按鈕后面的對象,可以在單擊事件處理程序中執行以下操作:

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

要使數據源中的更改自動更新到列表框,可以使用ObservableCollection。

使用ObservableCollection查看即時自動更改

使用DataContext獲取SelectedItem

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

您應該在viewmodel中將SelectedItem屬性綁定到列表框的selectedItem。 從ObservableCollection中刪除該特定項。 您的列表框將反映更改。

問題2:使用ObservableCollection。 這實現了INotifyCollectionChanged,因此在集合更改時將添加和刪除項目。

問題1:將發件人轉換為按鈕並使用它的DataContext。 這將是它所綁定的項目。 如果您還需要獲取ListBoxItem本身,您可以使用VisualTreeHelper.GetParent()來搜索樹。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM