2013-10-02 87 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