Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding to the current item from within the ItemTemplate

Tags:

wpf

Is it possible to bind the current item to a tag in button from a listview itemsource like this. How do i do it Please help

<ListView ItemsSource="Assignments">
      <ListView.ItemTemplate>
          <DataTemplate>
              <Button Tag="{Binding CurrentItem}">

              </Button>
          </DataTemplate>
      </ListView.ItemTemplate>
</ListView>
like image 727
Tan Avatar asked Dec 16 '22 02:12

Tan


2 Answers

Yes, if Assignments is a collection of string then you need

<Button Tag="{Binding}">

If Assignments is a collection of Assignment where Assignment has a property Name you need

<Button Tag="{Binding Name}">
like image 100
Phil Avatar answered Jan 06 '23 08:01

Phil


If by "current item" you mean the currently selected item:

<Button Tag="{Binding SelectedItem, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ListView}}}"/>

If by "current item" you mean the data item being rendered by the template:

<Button Tag="{Binding}"/>
like image 45
Kent Boogaart Avatar answered Jan 06 '23 10:01

Kent Boogaart