1 <UserControl x:Class="WpfApplication1.autoBox" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 5 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 6 xmlns:local="clr-namespace:WpfApplication1" 7 mc:Ignorable="d" 8 d:DesignHeight="300" d:DesignWidth="300"> 9 <UserControl.Resources> 10 <Style TargetType="ListBox"> 11 <Setter Property="BorderThickness" Value="1"/> 12 <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/> 13 <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/> 14 <Setter Property="ScrollViewer.CanContentScroll" Value="true"/> 15 <Setter Property="VerticalContentAlignment" Value="Center"/> 16 </Style> 17 <Style TargetType="ListBoxItem"> 18 <Setter Property="Foreground" Value="Black" /> 19 <Setter Property="Margin" Value="2"/> 20 <Setter Property="FontSize" Value="13" /> 21 <Style.Triggers> 22 <Trigger Property="IsSelected" Value="True"> 23 <Setter Property="Background" Value="#08246B"> 24 </Setter> 25 <Setter Property="Foreground" Value="White" /> 26 </Trigger> 27 </Style.Triggers> 28 <Style.Resources> 29 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#08246B" /> 30 <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#08246B" /> 31 </Style.Resources> 32 </Style> 33 <Style TargetType="Button"> 34 <Setter Property="FontSize" Value="13" /> 35 <Setter Property="Foreground" Value="Blue" /> 36 <Setter Property="BorderBrush" Value="Transparent"/> 37 <Setter Property="Background" Value="Transparent"/> 38 <EventSetter Event="Click" Handler="Button_LeftClick"/> 39 <Style.Triggers> 40 <Trigger Property="IsMouseOver" Value="True"> 41 <Setter Property="Background" Value="Yellow"/> 42 </Trigger> 43 </Style.Triggers> 44 </Style> 45 <Style x:Key="styleToStackPannel" TargetType="StackPanel"> 46 <Style.Triggers> 47 <Trigger Property="IsMouseOver" Value="True"> 48 <Setter Property="Background" Value="Yellow"/> 49 </Trigger> 50 </Style.Triggers> 51 <Style.Resources> 52 <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#08246B" /> 53 <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#08246B" /> 54 </Style.Resources> 55 </Style> 56 <ControlTemplate x:Key="dt"> 57 <Button Name="btn" Width="auto" Height="auto" > 58 <Button.Template> 59 <ControlTemplate> 60 <StackPanel Name="sp" Orientation="Horizontal" Style="{StaticResource styleToStackPannel}" > 61 <Image Name="img" Width="16" Height="16"></Image> 62 <TextBlock Text="新增"></TextBlock> 63 </StackPanel> 64 </ControlTemplate> 65 </Button.Template> 66 </Button> 67 </ControlTemplate> 68 </UserControl.Resources> 69 70 71 72 <StackPanel> 73 <TextBox Name="tb" LostFocus="tb_LostFocus"/> 74 <ListBox Name="lb" MaxHeight="150" Canvas.Left="0" Canvas.Top="20" Visibility="Collapsed"/> 75 </StackPanel> 76 77 </UserControl>
代码如上,主要是对listbox做了一些调整,第一项写成一个按钮,余项不改动,但是明明给按钮绑定了一个事件,为什么就是不触发呢,试了好多方法,都不行,求指点迷津
<EventSetter Event="Click" Handler="Button_LeftClick"/>没见过这么写,我记得<Style.Triggers>下面有EventTrigger的吧。
还有你说的第一项写成一个按钮,不是很明白什么意思。如果你改动ListBoxItem模板的话,那是每个item的样式。