2016-12-16 51 views
0

我正在寫一個小的關鍵應用程序,我正在使用一個列表框來動態可視化代碼列表。現在的問題是它需要在Windows 7上使用,我在該平臺上的可視化存在多個問題。使listboxitem無法選擇

我的代碼是:

<ListBox x:Name="listBoxSecrets" Margin="0,84,10,10" Background="{x:Null}" 
      HorizontalContentAlignment="Stretch" BorderBrush="{x:Null}" > 
<ListBox.ItemTemplate> 
    <DataTemplate> 
     <!--<Border BorderBrush="#FFF3560D" CornerRadius="3" BorderThickness="3" Margin="0,0,0,10">--> 
     <Border BorderBrush="White" CornerRadius="10" BorderThickness="10" Margin="0,0,0,10"> 
      <Grid Background="White"> 
       <Grid.RowDefinitions> 
        <RowDefinition Height="Auto" /> 
       </Grid.RowDefinitions> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="Auto" /> 
        <ColumnDefinition Width="*" /> 
        <ColumnDefinition Width="30" /> 
        <ColumnDefinition Width="30" /> 
       </Grid.ColumnDefinitions> 
       <StackPanel Uid="{Binding Path=ID}" Grid.Row="0" Grid.Column="0"> 
        ... 
       </StackPanel> 


       <Button ... Grid.Row="0" Grid.Column="2"> 
        ... 
       </Button> 
       <Button ... Grid.Row="0" Grid.Column="3" > 
        ... 
       </Button> 
      </Grid> 
     </Border> 
    </DataTemplate> 
</ListBox.ItemTemplate> 

我想下面的樣式沒有效果:

<Window.Resources> 
    <Style x:Key="{x:Type ListBox}" TargetType="{x:Type ListBox}"> 
     <Setter Property="Focusable" Value="False" /> 
     <Setter Property="FocusVisualStyle" Value="{x:Null}" /> 
    </Style> 
</Window.Resources> 

這是選擇的樣子 - 我不希望它可以選擇。 (我有一個按鈕同樣的問題順便說一句 - 同樣的解決方案將適用?)

前: enter image description here

後點擊: enter image description here

另外:獎勵積分,如果你能幫助我弄清楚如何去除網格和它周圍的邊界之間的「別名」線。只發生在贏得7場比賽,而不是我贏得8場比賽。

回答

1

你爲什麼不刪除發生的選擇?就像這樣:

private void listBoxSecrets_SelectionChanged(object sender, EventArgs e) 
{ 
    listBoxSecrets.SelectedIndex = -1; 
} 

然後,您可以設置監聽器是這樣的:

<ListBox x:Name="listBoxSecrets" Margin="0,84,10,10" Background="{x:Null}" 
     HorizontalContentAlignment="Stretch" BorderBrush="{x:Null}" SelectionChanged="listBoxSecrets_SelectionChanged" > 
+0

我怎麼會這個功能添加到列表框中單擊事件?因爲我通過DataTemplate創建列表框 – Spyral

+0

@Spyral請參閱編輯。 – Emad

+0

一些debugigng後,我發現我得到的故障實際上是​​由於邊界被選中!我將如何解決這個問題? – Spyral

1

我猜你想刪除一個ListBoxItem的選擇狀態,如果那是正確的,請使用以下的風格, 在這我已經評論了負責選擇和鼠標懸停狀態的觸發器。 您可以在列表框的ItemContainerStyle分配這種風格,,

<Style x:Key="FocusVisual"> 
     <Setter Property="Control.Template"> 
      <Setter.Value> 
       <ControlTemplate> 
        <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}" StrokeThickness="1" StrokeDashArray="1 2"/> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <SolidColorBrush x:Key="Item.MouseOver.Background" Color="#1F26A0DA"/> 
    <SolidColorBrush x:Key="Item.MouseOver.Border" Color="#a826A0Da"/> 
    <SolidColorBrush x:Key="Item.SelectedInactive.Background" Color="#3DDADADA"/> 
    <SolidColorBrush x:Key="Item.SelectedInactive.Border" Color="#FFDADADA"/> 
    <SolidColorBrush x:Key="Item.SelectedActive.Background" Color="#3D26A0DA"/> 
    <SolidColorBrush x:Key="Item.SelectedActive.Border" Color="#FF26A0DA"/> 
    <Style x:Key="ListBoxItemStyle1" TargetType="{x:Type ListBoxItem}"> 
     <Setter Property="SnapsToDevicePixels" Value="True"/> 
     <Setter Property="Padding" Value="4,1"/> 
     <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="Transparent"/> 
     <Setter Property="BorderThickness" Value="1"/> 
     <Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ListBoxItem}"> 
        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"> 
         <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </Border> 
        <ControlTemplate.Triggers> 
         <!--<MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="IsMouseOver" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.MouseOver.Background}"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.MouseOver.Border}"/> 
         </MultiTrigger>--> 
         <!--<MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="Selector.IsSelectionActive" Value="False"/> 
           <Condition Property="IsSelected" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Background}"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedInactive.Border}"/> 
         </MultiTrigger> 
         <MultiTrigger> 
          <MultiTrigger.Conditions> 
           <Condition Property="Selector.IsSelectionActive" Value="True"/> 
           <Condition Property="IsSelected" Value="True"/> 
          </MultiTrigger.Conditions> 
          <Setter Property="Background" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Background}"/> 
          <Setter Property="BorderBrush" TargetName="Bd" Value="{StaticResource Item.SelectedActive.Border}"/> 
         </MultiTrigger>--> 
         <Trigger Property="IsEnabled" Value="False"> 
          <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/> 
         </Trigger> 
        </ControlTemplate.Triggers> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
+0

之後加入這一行在經過一些debugigng之後,我發現我得到的小故障實際上是​​由於邊框被選中了!我將如何解決這個問題? – Spyral

+0

以上風格? – WPFUser

+0

我試過了,但它仍然給出了相同的問題。 – Spyral