ListBoxItem and DataTemplate containing a TextBlock RRS feed

  • Question




    Changed the ItemTemplate of a ListBox to a Canvas that contains a ViewBox that Contains a TextBlock


    When I click any where on the Canvas the item get selected. If I click on the TextBlock the item doesn't get selected.


    Does the TextBlock prevent the click to go any further?


    Thank you by advance

    Friday, May 2, 2008 5:37 PM

All replies

  • It depends on how the TextBlock is defined in your template. I have run into a problem with this when I had a TextBlock that we were planning on using as a clickable element in the future and had added IsHitTestVisible="True" to it which prevented anything beneath it from taking focus. You may want to check IsHitTestVisible and Focusable on both your TextBlock and ViewBox to make sure that they cannot receive focus.
    Friday, May 2, 2008 6:38 PM
  • Hello,


    Thanks  IsHitTestVisible="False" on the TextBlock does indeed make the ListBoxItem selectable.

    But if I do that the event DragDelta does not fire any more.


    If I set IsHitTestVisible="True" the item is not selectable by clicking on the TextBlock and the DragDelta event works only when dragging the TextBlock


    the template is something like :


    Code Snippet


    <DataTemplate >

    <Thumb DragDelta="MoveListBoxItem">



    <Canvas Name="_itemCanvas" Height="25" Width="100" >


    Width="{Binding ElementName=_itemCanvas,Path=Width}"

    Height="{Binding ElementName=_itemCanvas,Path=Height}">

    <TextBlock IsHitTestVisible="True" Text="Hello World"/>










    So what I don't get is:

    1. Why the DragDelta is raised only on the TextBlock
    2. Why if I set IsHitTestVisible="False" the DragDelta is not raised any more
    3. Why if I set IsHitTestVisible="True" the ListboxItem is only selectable when clicking outside of the TextBlock

    If someone has an idea to get the DragDelta and the ListBoxItem selectable working It would be more than welcome. Smile


    Thank you

    Friday, May 2, 2008 8:43 PM
  •  Gomata wrote:

    When I click any where on the Canvas the item get selected. If I click on the TextBlock the item doesn't get selected.

    You could try subscribing to TextBlock.Preview
    MouseLeftButtonDown event, and programmatically select the ListBoxItem which contains the currently clicked TextBlock as follows:
    private void textBlock_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        ListBoxItem item = ItemsControl.ContainerFromElement(listBox, e.OriginalSource as DependencyObject) as ListBoxItem;
        if (item != null)
            item.IsSelected = true;

    Hope this helps
    Monday, May 5, 2008 6:01 AM

    Thank you for the help


    I would like to avoid dealing myself with the events to select the item. It sounds complex to me. Because I will need to handle selection, unselection, CTRL + click and so on.



    I notice that the Background seems to involved in this process.


    If I set the IsHitTestVisible="false" on the text box and if I add a background="Transparant" to the Canvas then I get the drag event from any where on the canvas but the item doesn't get selected.


    do I need to add a border or a background somewhere to get both behaviour working?


    Thank you

    Monday, May 5, 2008 11:27 AM