user2837961
user2837961

Reputation: 1555

Index of selected item in ItemsControl

In my xaml I have a ItemControl which internally has some controls on top eg wrappanel and Buttons. When the user click on the button, how can I know its itemindex?

<ItemsControl ItemsSource="{Binding ConditionList}" AlternationCount="{Binding ConditionList.Count}">
    <ItemsControl.ItemTemplate>
       <DataTemplate>
          <WrapPanel Background="#FFB1CBCB">
            <ComboBox ItemsSource="{Binding DataContext.NodeMembershipFunction, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}, AncestorLevel=1}}" DisplayMemberPath="_Name" SelectedValue="{Binding Condition, Mode=TwoWay}" SelectedValuePath="_Type"></ComboBox>
            <Button Content="Remove" Click="Remove_Click" />
          </WrapPanel>
        </DataTemplate>
     </ItemsControl.ItemTemplate>
  </ItemsControl>

Upvotes: 2

Views: 1080

Answers (1)

Amit Raz
Amit Raz

Reputation: 5536

Using events is highly not recommended in ItemTemplates as it breaks the whole idea of MVVM and DataTemplates. You should Bind that Button to a Command which will be implemented inside each item in the ConditionList.

By looking at your code I see that you are trying to remove the item. I dont know how your items are implemented but you should do it though a Command inside your viewmodel. You can pass a service to each of the items and have each of them request itself removed, Just a though...

Upvotes: 1

Related Questions