2014-01-06 76 views
0

我正在使用列表框來顯示我在聲控程序中以編程方式選擇的項目列表。有沒有辦法保持所選項目不被點擊?我希望鼠標懸停在功能上,而不是點擊實際的項目。如何使WPF列表框項目不可點擊

我試圖設置調焦(沒有做我想要的任何東西)和的IsEnabled(禁用鼠標)

這裏是我當前的風格:

<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> 
+0

只是通過設置ItemContainerStyle禁用ItemContainer。你有什麼嘗試? –

+0

更新了我的答案....禁用容器使鼠標不起作用 –

+0

類似的問題http://stackoverflow.com/questions/20957029/how-to-stop-user-from-selecting-items-in-a-listbox -without-disable-it – Paparazzi

回答

1
<ListBox PreviewMouseDown="ListBox_OnPreviewMouseDown".. 

private void ListBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e) 
{ 
    e.Handled = true; 
} 
+1

我發誓我找了一隻老鼠....只是失明瞭。我有一個單獨的解決方案,我會發布,但我更喜歡這個更好,因爲這是一個UI關注的問題,而不是我應該在VM中處理的東西 –

0

的替代解決方案我接受的是在我的視圖模型中添加以下內容:

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

然後,s在實際設置SelectedGlyph之前,每次都調用_viewModelSetSelectedGlyph。然後,當UI試圖設置SelectedGlyph時,它不會成功,只有當視圖模型通過設置私有變量來設置它纔會成功。

相關問題