2017-07-27 68 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 
     } 
相關問題