对不起,如果这太原始了,但我搜索,并没有找到任何解决我的问题。如何将鼠标事件添加到自定义模板列表框项目?
我有这样的控制模板中我的列表框项目:
<ControlTemplate TargetType="{x:Type ListBoxItem}">
<Grid>
<Border x:Name="outerBorder" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" SnapsToDevicePixels="true">
<Border x:Name="innerBorder" Background="{TemplateBinding Background}" BorderThickness="1" CornerRadius="0" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<DockPanel LastChildFill="False" >
<StackPanel Orientation="Horizontal" DockPanel.Dock="Left">
<Label x:Name="iconi" Content="#" Foreground="Red"/>
<ContentPresenter Content="{Binding YearClass}" ContentSource="Binding YearClass" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
</StackPanel>
<Border x:Name="NumBorder" MinWidth="20" Height="20" DockPanel.Dock="Right" Background="#8395bb" CornerRadius="10" >
<Label x:Name="BookNum" Content="{Binding Path=NumbOfBook}" Foreground="#ffffff" FontSize="10" />
</Border>
</DockPanel>
</Border>
</Border>
</Grid>
而这种代码使列表框的数据源:
public void fill_lib()
{
List<YearBook> yeartitles = new List<YearBook>();
yeartitles.Add(new YearBook() { xContent ="One", YearClass = "first year", NumbOfBook = 17, selectlink = "openWind" });
yeartitles.Add(new YearBook() { xContent = "Two", YearClass = "second year", NumbOfBook = 5, selectlink = "showItem" });
yeartitles.Add(new YearBook() { xContent = "three", YearClass = "third year", NumbOfBook = 14, selectlink = "dataTemp" });
middleone.ItemsSource = yeartitles;
}
我的问题是如何添加鼠标点击事件或选定的事件两个我的列表项?
你不需要重写ListBoxItem.Template显示自定义字段。只需使用ListBox的ItemTemplate属性即可。具有与DataContext绑定的ControlTemplate(如'“{Binding Path = NumbOfBook}”')几乎不可重复使用 – ASh
哪些鼠标事件和您想要的目的?考虑使用SelectedItem属性与SelectedItemChanged事件或考虑InputBindings – ASh
对不起,我编辑了我关于我想要的事件的最后一行问题。选择或鼠标单击事件 – hemarn