0

I am using a listbox to display a list of items that I am selecting programmatically in a voice activated program. Is there any way to keep the selected item from being clicked on? I do want mouse over functionality, just not clicking on the actual item.

I have tried to set Focusable (does not do anything for what I want) and IsEnabled (disables mouse over)

Here is my current style:

<Style x:Key="GlyphList" TargetType="ListBox">
    <Setter Property="ItemsPanel">
      <Setter.Value>
        <ItemsPanelTemplate>
          <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" IsEnabled="False"/>
        </ItemsPanelTemplate>
      </Setter.Value>
    </Setter>
    <Setter Property="ItemTemplate">
      <Setter.Value>
        <DataTemplate DataType="models:SpellingGlyph">
          <Label VerticalAlignment="Bottom">
            <Label.Template>
              <ControlTemplate>
                <Grid>
                  <TextBlock Name="MainText" Text="{Binding Text}" VerticalAlignment="Bottom" Margin="0,0,5,0"/>
                </Grid>
                <ControlTemplate.Triggers>
                  <DataTrigger Value="True">
                    <DataTrigger.Binding>
                      <MultiBinding Converter="{StaticResource ObjectReferenceEqualityConverter}">
                        <Binding />
                        <Binding Path="SelectedItem" RelativeSource="{RelativeSource FindAncestor, AncestorType=ListBox}"/>
                      </MultiBinding>
                    </DataTrigger.Binding>
                    <Setter TargetName="MainText" Property="Foreground" Value="#75BAFF"/>
                    <Setter TargetName="MainText" Property="FontWeight" Value="SemiBold"/>
                    <Setter TargetName="MainText" Property="FontSize" Value="22"/>
                  </DataTrigger>
                  <DataTrigger Value="True">
                    <DataTrigger.Binding>
                      <MultiBinding Converter="{StaticResource MultiBooleanConverter}"> <!--SelectedItem trumps mouse over-->
                        <Binding ElementName="MainText" Path="IsMouseOver"/>
                        <Binding Path="SelectedItem" RelativeSource="{RelativeSource FindAncestor, AncestorType=ListBox}" Converter="{StaticResource InvertedNullCheckToBooleanConverter}"/>
                      </MultiBinding>
                    </DataTrigger.Binding>
                    <Setter TargetName="MainText" Property="Foreground" Value="#75BAFF"/>
                    <Setter TargetName="MainText" Property="FontWeight" Value="SemiBold"/>
                    <Setter TargetName="MainText" Property="FontSize" Value="22"/>
                  </DataTrigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Label.Template>
          </Label>
        </DataTemplate>
      </Setter.Value>
    </Setter>
  </Style>
Justin Pihony
  • 66,056
  • 18
  • 147
  • 180

3 Answers3

2
<ListBox PreviewMouseDown="ListBox_OnPreviewMouseDown"..

private void ListBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    e.Handled = true;
}
denis morozov
  • 6,236
  • 3
  • 30
  • 45
  • 1
    I swear I looked for a mouse down....just was blind. I had a separate solution that I will post, but I actually like this better as this is more of a UI concern than something I should handle in the VM – Justin Pihony Jan 09 '14 at 18:49
1

Simply set IsHitTestVisible to false

https://msdn.microsoft.com/en-us/library/system.windows.uielement.ishittestvisible(v=vs.110).aspx

Tony Wu
  • 1,040
  • 18
  • 28
0

An alternative solution to what I accepted was to add the following in my view model:

     if (_viewModelSetSelectedGlyph != value) return;
    _selectedGlyph = value;
    OnPropertyChanged("SelectedGlyph");

Then, set the _viewModelSetSelectedGlyph each time before actually setting the SelectedGlyph. Then, when the UI tries to set the SelectedGlyph, it would not succeed, only when the view model sets it by also setting the private variable does it succeed.

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180