2017-07-27 139 views
0

对不起,如果这太原始了,但我搜索,并没有找到任何解决我的问题。如何将鼠标事件添加到自定义模板列表框项目?

我有这样的控制模板中我的列表框项目:

<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; 
    } 

我的问题是如何添加鼠标点击事件或选定的事件两个我的列表项?

+0

你不需要重写ListBoxItem.Template显示自定义字段。只需使用ListBox的ItemTemplate属性即可。具有与DataContext绑定的ControlTemplate(如'“{Binding Path = NumbOfBook}”')几乎不可重复使用 – ASh

+0

哪些鼠标事件和您想要的目的?考虑使用SelectedItem属性与SelectedItemChanged事件或考虑InputBindings – ASh

+0

对不起,我编辑了我关于我想要的事件的最后一行问题。选择或鼠标单击事件 – hemarn

回答

1

您不必重载ListBoxItem模板来显示自定义字段。通过使用ListBox的ItemTemplate属性,你可以得到你想要的。是这样的:

<ListBox x:Name="mainbox" > 
      <ListBox.ItemTemplate> 
       <DataTemplate> 
        <StackPanel Orientation="Horizontal" Margin="0,0,0,5"> 
         <Rectangle Width="15" Height="15" Fill="Green" Margin="0,0,5,0"/> 
         <TextBlock Text="{Binding BaseName}" Margin="0,0,5,0" /> 
         <Border CornerRadius="12" Background="#FFB05656" MinWidth="15"> 
          <TextBlock Text="{Binding BaseBookCount}" /> 
         </Border> 
        </StackPanel> 
       </DataTemplate> 
      </ListBox.ItemTemplate> 
     </ListBox> 

在这种情况下,您可以访问列表框的事件,如果你想检索一个ListBoxItem的数据,你可以使用此代码:

private void mylst_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      //recive item data as user type (that I defined) 
      var text = (mylst.SelectedItem as User); 
      Console.WriteLine(text.Name.ToString()+ " "+ text.Mail.ToString()); 
     } 
0

查看可以处理鼠标点击的事件处理程序。以下是一些示例代码,只要您更改ListBox中的选定索引就会做出反应。

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      //do something 
     } 
相关问题