none
AutoCompleteBox: Issue with IsHitTestVisible

    Question

  • Hello,

    I use the AutoCompleteBox. Since I use partly the AutoCompleteBox also as a menu(dependent on the context) I have a listBoxItem which acts as a separtor. This works fine. What I now want to achieve is that user can not select such separators list box items.

    I've tried to achieve this by using the IsHitTestVisible Property.

    <Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="ListBoxItem">
                            <ContentPresenter IsHitTestVisible="{Binding IsHitTestVisible}"/>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
    ...
    Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" ScrollViewer.HorizontalScrollBarVisibility="Auto" ItemTemplate="{TemplateBinding ItemTemplate}" ScrollViewer.VerticalScrollBarVisibility="Auto"                                                    
                                                            ItemsSource="{Binding InputAssistentControlModel.Items}" 
                                                           ItemContainerStyle="{StaticResource ListBoxItemStyle}"                                                         Width ="400" Loaded="Selector_Loaded"  >
                                                </ListBox>
                                            </Border>

    The separator in the data template

    <!--ItemSeparatorTemplate -->
                <view:IpaTemplateSelector.ItemSeparatorTemplate>
                    <DataTemplate>
                        <Grid SizeChanged="Separator_SizeChanged">
                            <Rectangle HorizontalAlignment="Stretch" Stretch="Fill" Height="1" Fill="Black" />
                        </Grid>
                    </DataTemplate>
                </view:IpaTemplateSelector.ItemSeparatorTemplate>

    This listBox template is then used as a template for the AutoCompleteBox.

    Now I face the issue that during runtime all listBoxItems are now disabled, although only one item during item (the separtor) is (IsHitTestVisible = false).

    The property is defined in the ItemSourceType as

    public bool IsHitTestVisible { get; set; }
    

     

    I checked the binding(in the Style ListBoxItemStyle) in the debugger...the items are correctly present in the ItemSourceProperty

     

    Anybody can help me here or has an alternative idea how to accomplish this 'feature'? I've tried to use ItHitTestVisible in the DataTemplate (in the Grid element) but it did not work there.

    Friday, January 27, 2012 7:21 AM

Answers

  • My solution is:

    <Style x:Key="modItemMainParts" TargetType="ListBoxItem">
       
    <Setter Property="Template">
           
    <Setter.Value>
               
    <ControlTemplate TargetType="ListBoxItem">
                   
    <Grid x:Name="g" Background="{TemplateBinding Background}">

                       
    ...

                       
    <i:Interaction.Triggers>
                           
    <ei:DataTrigger Binding="{Binding abilitato}" Value="False">
                               
    <ei:ChangePropertyAction TargetObject="{Binding ElementName=g}" TargetName="IsHitTestVisible" Value="False" PropertyName="IsHitTestVisible"/>
                               
    <ei:ChangePropertyAction TargetObject="{Binding ElementName=g}" TargetName="Opacity" Value="0.5" PropertyName="Opacity"/>
                           
    </ei:DataTrigger>
                       
    </i:Interaction.Triggers>
                     
    </Grid>
               
    </ControlTemplate>
           
    </Setter.Value>
       
    </Setter>
    </Style>

    The conclusions are presonal and certainly some problems can be worked around, but...

    • In contrast to WPF, in Silverlight a DataTrigger (with its ChangePropertyAction) can't stay elsewhere then inside a control inside the Template. And the property TargetObject of the ChangePropertyAction object, can be binded only linking the name of a control inside the template (TargetObject="{Binding ElementName=g}" )

    • For this reason it's impossible to bind the DataTrigger with the entire Item and to set it's property IsEnabled but we need to work with every single control, and we need to reproduce the behaviour of the Item when it's disabled. Particularly we can set the property IsHitTestVisible of the main Grid, that contains every other controls, to False, to avoid that it were responsible for the mouse inputs.

    Finally in WPF it was quite more easy, but the important is to know what to do.

    Saturday, January 28, 2012 3:40 AM
  • Thx for your answer. I have not checked it in detail since I found that solution in the meantime here

    http://social.msdn.microsoft.com/Forums/en-US/silverlightcontrols/thread/380ca1ca-42e0-4bc1-90e0-655deb0400cb//1

    which is sufficient for me.

     

    Monday, January 30, 2012 9:42 AM

All replies

  • My solution is:

    <Style x:Key="modItemMainParts" TargetType="ListBoxItem">
       
    <Setter Property="Template">
           
    <Setter.Value>
               
    <ControlTemplate TargetType="ListBoxItem">
                   
    <Grid x:Name="g" Background="{TemplateBinding Background}">

                       
    ...

                       
    <i:Interaction.Triggers>
                           
    <ei:DataTrigger Binding="{Binding abilitato}" Value="False">
                               
    <ei:ChangePropertyAction TargetObject="{Binding ElementName=g}" TargetName="IsHitTestVisible" Value="False" PropertyName="IsHitTestVisible"/>
                               
    <ei:ChangePropertyAction TargetObject="{Binding ElementName=g}" TargetName="Opacity" Value="0.5" PropertyName="Opacity"/>
                           
    </ei:DataTrigger>
                       
    </i:Interaction.Triggers>
                     
    </Grid>
               
    </ControlTemplate>
           
    </Setter.Value>
       
    </Setter>
    </Style>

    The conclusions are presonal and certainly some problems can be worked around, but...

    • In contrast to WPF, in Silverlight a DataTrigger (with its ChangePropertyAction) can't stay elsewhere then inside a control inside the Template. And the property TargetObject of the ChangePropertyAction object, can be binded only linking the name of a control inside the template (TargetObject="{Binding ElementName=g}" )

    • For this reason it's impossible to bind the DataTrigger with the entire Item and to set it's property IsEnabled but we need to work with every single control, and we need to reproduce the behaviour of the Item when it's disabled. Particularly we can set the property IsHitTestVisible of the main Grid, that contains every other controls, to False, to avoid that it were responsible for the mouse inputs.

    Finally in WPF it was quite more easy, but the important is to know what to do.

    Saturday, January 28, 2012 3:40 AM
  • Thx for your answer. I have not checked it in detail since I found that solution in the meantime here

    http://social.msdn.microsoft.com/Forums/en-US/silverlightcontrols/thread/380ca1ca-42e0-4bc1-90e0-655deb0400cb//1

    which is sufficient for me.

     

    Monday, January 30, 2012 9:42 AM