2013-10-02 88 views
0

我一直試圖刪除藍色框,當鼠標在Listbox中的項目結束,我跑出了想法,也許你會出來任何。先謝謝你。 enter image description hereMouseOver刪除ListboxItem的藍色邊框

簡單列表框

<ListBox ItemsSource="{Binding Mylist}" /> 

不幸的是,解決方案如下不起作用

<ListBox ItemsSource="{Binding lista}" > 
     <ListBox.ItemContainerStyle> 
      <Style TargetType="{x:Type ListBoxItem}"> 
       <Style.Resources> 
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="Transparent"/> 
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/> 
       </Style.Resources> 
      </Style> 
     </ListBox.ItemContainerStyle> 

    </ListBox> 

enter image description here

回答

1

此行爲是由控制模板支配。

如果您對XAML熟悉,請右鍵單擊列表框,轉至Edit Template -> Edit Copy...檢查Border標籤。

爲了幫助你,檢查此鏈接,以及:ListBox Styles and Templates

+0

讓您的回覆更加精確。 – Maximus

+0

克里斯W反應完成我的。 – Tico

0

新的問題出來了,我獲得了列表框沒有藍色邊框

<Style TargetType="ListBoxItem"> 
     <Setter Property="HorizontalContentAlignment" Value="Left" /> 
     <Setter Property="VerticalContentAlignment" Value="Top" /> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ListBoxItem"> 
        <Grid Background="{TemplateBinding Background}"> 
         <ContentPresenter 
          x:Name="contentPresenter" 
          Content="{TemplateBinding Content}" 
          ContentTemplate="{TemplateBinding ContentTemplate}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          Margin="{TemplateBinding Padding}"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

,但我已經設置ItemContainerStyle這樣

<Style TargetType="ListBoxItem" x:Key="ContainerStyle"> 
     <Setter Property="ContentTemplate" Value="{StaticResource not_mouseover}"/> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="ContentTemplate" Value="{StaticResource mouseover}"/> 
      </Trigger> 

     </Style.Triggers> 
    </Style> 
    <ListBox ItemsSource="{Binding lista}" ItemContainerStyle="{StaticResource ContainerStyle}"> 

在這種情況下,它證明它不起作用(我的意思是藍色邊框如前所示)。如果我將ItemTemplate設置爲任何指定的DateTemplate,它可以正常工作,但在這裏不是。你碰巧知道爲什麼? 我整理了一下。只是一個風格ListBoxItem的

<Style x:Key="item_template" TargetType="ListBoxItem"> 
     <Style.Triggers> 
      <Trigger Property="IsMouseOver" Value="True"> 
       <Setter Property="Template" Value="{StaticResource control_mouseover}"/> 
      </Trigger> 
      <Trigger Property="IsMouseOver" Value="False"> 
       <Setter Property="Template" Value="{StaticResource control_not_mouseover}"/> 
      </Trigger> 
     </Style.Triggers>   
    </Style> 
</Window.Resources> 
    <ListBox ItemsSource="{Binding lista}" ItemContainerStyle="{StaticResource item_template}"> 
    </ListBox> 

,爲了去除藍色邊框

<ControlTemplate x:Key="control_not_mouseover" TargetType="ListBoxItem"> 
     <ContentPresenter 
          Content="{TemplateBinding Content}" 
          ContentTemplate="{StaticResource not_mouseover}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          Margin="{TemplateBinding Padding}"/> 
    </ControlTemplate> 
    <ControlTemplate x:Key="control_mouseover" TargetType="ListBoxItem"> 
      <ContentPresenter 
          Content="{TemplateBinding Content}" 
          ContentTemplate="{StaticResource mouseover}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          Margin="{TemplateBinding Padding}"/> 
     </ControlTemplate> 

也許有人會利用這個聲明的ControlTemplate。

0

沒有x:Key的樣式可用於所有TargetType控件。
例如:

<Style TargetType="Button"> 
     <Setter Property="Background" Value="Green" /> 
</Style> 

將工作每次設定一個新的Button控制時間。因此,如果您插入Button而未指定如下樣式:<Button/>它將具有綠色背景,如上所述。

在另一方面:

<Style TargetType="Button" x:Key="myButton"> 
     <Setter Property="Background" Value="Green" /> 
</Style> 

將只Button控制指定Style模板工作。
例如:<Button Style="{StaticResource myButton}" /> - >此Button將具有綠色背景,所有其他按鈕將具有默認背景色。

我的建議是:總是在您的樣式中設置一個x:Key,以便稍後設置它們。在您的場景中,將放在第一個代碼處,並刪除稍後聲明的樣式。它應該工作。

+0

我更新了包含解決方案的前一封郵件。感謝努力並犧牲了時間。 – Maximus

+0

我上面的回答有用嗎?如果是這樣,你可以將它設置爲答案嗎?感謝和偉大的工作! – Tico