none
Access a control in datatemplate of a ListBox does raise SelectionChanged Event?

    Question

  • In the datatemplate of a ListBox, I place a TextBox and a Button.

    When I click the Button, or focus into the TextBox,  the selected Item of the ListBox does not change at all. The only way to change the selected item is to click the item itself.

    So how can I change the selected item when I access the controls in datatemplate?

    Monday, September 11, 2006 8:24 AM

Answers

  • this would work

    <Style x:Key="st1" TargetType="{x:Type ListBoxItem}">

    <Setter Property="Template">

    <Setter.Value>

    <ControlTemplate TargetType="{x:Type ListBoxItem}">

    <Border Name="Border" Padding="2">

    <StackPanel Orientation="Horizontal">

    <Button Name="btn1">

    <ContentPresenter Content="{TemplateBinding Content}"/>

    </Button>

    <TextBox Name="txt1" Width="150" Text="{TemplateBinding Content}"></TextBox>

    </StackPanel>

    </Border>

    <ControlTemplate.Triggers>

    <Trigger SourceName="txt1" Property="IsFocused" Value="True">

    <Setter Property="IsSelected" Value="True"></Setter>

    <Setter TargetName="Border" Property="Background" Value="Yellow"></Setter>

    </Trigger>

    <Trigger SourceName="btn1" Property="IsFocused" Value="True">

    <Setter Property="IsSelected" Value="True"></Setter>

    <Setter TargetName="Border" Property="Background" Value="Yellow"></Setter>

    </Trigger>

    </ControlTemplate.Triggers>

    </ControlTemplate>

    </Setter.Value>

    </Setter>

    </Style>

    Monday, September 11, 2006 9:34 AM

All replies

  • this would work

    <Style x:Key="st1" TargetType="{x:Type ListBoxItem}">

    <Setter Property="Template">

    <Setter.Value>

    <ControlTemplate TargetType="{x:Type ListBoxItem}">

    <Border Name="Border" Padding="2">

    <StackPanel Orientation="Horizontal">

    <Button Name="btn1">

    <ContentPresenter Content="{TemplateBinding Content}"/>

    </Button>

    <TextBox Name="txt1" Width="150" Text="{TemplateBinding Content}"></TextBox>

    </StackPanel>

    </Border>

    <ControlTemplate.Triggers>

    <Trigger SourceName="txt1" Property="IsFocused" Value="True">

    <Setter Property="IsSelected" Value="True"></Setter>

    <Setter TargetName="Border" Property="Background" Value="Yellow"></Setter>

    </Trigger>

    <Trigger SourceName="btn1" Property="IsFocused" Value="True">

    <Setter Property="IsSelected" Value="True"></Setter>

    <Setter TargetName="Border" Property="Background" Value="Yellow"></Setter>

    </Trigger>

    </ControlTemplate.Triggers>

    </ControlTemplate>

    </Setter.Value>

    </Setter>

    </Style>

    Monday, September 11, 2006 9:34 AM
  • I'm not sure this post answers anything.  What is this style being applied to?  I assume that it should be applied to the ItemContainerStyle property of a ListBox.  When this style is applied, the list box selection behaves erratically.  Consider this following event handler that will run when the selectedItem in a ListBox has changed:

    void
    listBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        MessageBox.Show(e.AddedItems.Count.ToString());
        MessageBox.Show(e.RemovedItems.Count.ToString());
    }

    In a listBox where only single selection is allowed normal behavior when selecting a different item would be to see a 2 message boxes, where both say "1".  This is noting that 1 item has now been selected and that one item is now not selected. 

    When implementing the XAML Lee D described, the following happens when clicking a button that is embedded in a ListBoxItem:  4 messages are displayed, so the event is firing twice.  The first time the messageboxes pair has the values 0 and 1, meaning that 0 items have been selected and one item has been unselected.  This means that the SelectedItem property of the ListBox is null, which crashes my application.  The second time the event fires (if the SelectedItem property is not referenced), the msgboxes have the values 1 and 0, meaning that 1 new item has been added and 0 have been removed. 

    Why is this happening like this?  How come clicking a button does not cause that item to be selected without any triggers?

    KP


    KP
    Wednesday, August 06, 2008 9:37 PM
  • I have the same problem, is there any solution now?
    Sunday, February 20, 2011 4:24 PM