locked
Check if Item in ItemsControl is Selected RRS feed

  • Question

  • Hello,

    i created a ItemsControl. Now i want check in ItemsControl.ItemTemplate if the Item is selected. Have anyone a idea, how i can access the value IsSelected?

    The problem is that the ITemsControl got nothing like ListViewItem or ListBoxItem. So something like:

    <ItemsControl.ItemTemplate>
    <DataTemplate>
        <DockPanel>
            <TextBlock Text="{Binding Path=Name}" DockPanel.Dock="Left"></TextBlock>
            <DockPanel.Style>
                <Style>
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding Path=IsSelected,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type Item}}}">
                            <Setter Property="Background" Value="Green"></Setter>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </DockPanel.Style>
        </DockPanel>
    </DataTemplate>
    </ItemsControl.ItemTemplate>
    Have anyone a idea how i can Check if the current item is Checked?

    greetings

    Thursday, August 27, 2009 10:07 AM

Answers

  • ItemsControl : Only knows how to present a list of items
        '--> Selector : Inherits from ItemsControl and adds the selection features, but has no presentation by itself
            '--> ListBox / ComboBox : Inherits from Selector and knows how to present its (selected) items using a scrollable (dropdown) list

    An ItemsControl puts any item inside a standard ContentPresenter and knows nothing about selection.
    A ListBox wraps any item in a ListBoxItem and both the ListBox and the ListBoxItem do know about selection.

    So, if you use a ListBox you can check for an AncestorType of ListBoxItem.

    It doesn't help to tell your ItemsControl to use ListBoxItem, because the it still doesn't know about selection at all.
    Inheriting from Selector is also difficult, because you'll have to do all the presentation and mouse-handling yourself. Most of the times it's easier to inherit from ListBox and strip everything from its controltemplate that you don't need.

    And if anyone wants to know more about the ItemsControl and its decendants, I always refer them to this series of articles by Dr. WPF:

    hth,
    Marcel
    • Marked as answer by mfelive.de Tuesday, September 1, 2009 11:57 AM
    Monday, August 31, 2009 3:09 PM

All replies

  • That's because ItemsControls is the base class for controls like ListView And ListBox.
    In between this inheritance there's System.Windows.Controls.Primitives.Selector that is the first to hold properties like SelectedIndex, SelectedItem and SelectedValue.

    Having said that either you go further down the inheritance and use a control that already holds the properties you need or you implement your ItemsControl. Maybe templating ListView is the best option for you.
    Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
    • Proposed as answer by martin.doku Friday, August 28, 2009 11:02 AM
    Thursday, August 27, 2009 10:35 AM
  • Hm

    so it would be easier if i will use a listbox instead of itemscontrol.

    Is it possible to tell my itemscontrol, that it should use as ItemsContainer the ListBoxItem? Or my Own ItemsClass which inheritance System.Windows.Controls.Primitves.Selector?
    Friday, August 28, 2009 11:02 AM
  • Sure it is.

    ItemsControl have some wonder properties, like ItemsPanel, that allow you to play around with that kind of stuff. Check this out:
    http://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemspanel.aspx


    Bigsby, Lisboa, Portugal - O que for, quando for, é que será o que é... http://bigsby.eu
    Monday, August 31, 2009 1:37 PM
  • ItemsControl : Only knows how to present a list of items
        '--> Selector : Inherits from ItemsControl and adds the selection features, but has no presentation by itself
            '--> ListBox / ComboBox : Inherits from Selector and knows how to present its (selected) items using a scrollable (dropdown) list

    An ItemsControl puts any item inside a standard ContentPresenter and knows nothing about selection.
    A ListBox wraps any item in a ListBoxItem and both the ListBox and the ListBoxItem do know about selection.

    So, if you use a ListBox you can check for an AncestorType of ListBoxItem.

    It doesn't help to tell your ItemsControl to use ListBoxItem, because the it still doesn't know about selection at all.
    Inheriting from Selector is also difficult, because you'll have to do all the presentation and mouse-handling yourself. Most of the times it's easier to inherit from ListBox and strip everything from its controltemplate that you don't need.

    And if anyone wants to know more about the ItemsControl and its decendants, I always refer them to this series of articles by Dr. WPF:

    hth,
    Marcel
    • Marked as answer by mfelive.de Tuesday, September 1, 2009 11:57 AM
    Monday, August 31, 2009 3:09 PM