對不起,如果這太原始了,但我搜索,並沒有找到任何解決我的問題。如何將鼠標事件添加到自定義模板列表框項目?
我有這樣的控制模板中我的列表框項目:
<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