如何将鼠标事件添加到自定义模板ListBox项目?

时间:2017-07-27 18:15:06

标签: c# wpf events listbox

很抱歉,如果这太原始了,但我搜索了一下,发现什么都没有解决我的问题。

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

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

此代码为列表框创建数据源:

public MyMessage(Session session, String fromdomain, String format, 
        String blastid, String listid, String offerid, int blastinstanceid,
        String displayname, String displayfrom, String mailfrom, String email, String subject,
        String encodingtype, String content) {
        super(session);
        this.session=session;
        this.fromdomain = fromdomain;
        this.format = format;
        this.blastid = blastid;
        this.listid = listid;
        this.offerid = offerid;
        this.blastinstanceid = blastinstanceid;
        this.displayname = displayname;
        this.displayfrom = displayfrom;
        this.mailfrom = mailfrom;
        this.email = email;
        this.subject = subject;
        this.content = content;
        try{
            setFrom(getAddress(displayfrom));
            setSentDate(new Date());
            setRecipients(RecipientType.TO, email);
            setSubject(subject);
            setReplyTo(getAddress2(mailfrom));
            setHeader("Message-Id", getUniqueMessageIDValue(session, 
fromdomain, format, blastid, listid, offerid, blastinstanceid));
        }catch(Exception e){
            System.out.println(e);
        }
    }
    @Override
    public void writeTo(OutputStream out, String[] ignoreList) throws 
java.io.IOException, MessagingException{
        LineOutputStream los = null;
        try{
            if (!saved)
                saveChanges();

        String replyto = ("\""+displayname+"\" <"+displayfrom+">");
        String fromheader = ("\""+displayname+"\" <"+mailfrom+">");
        los = new LineOutputStream(out);
        los.writeln("Date: "+getHeader("Date", null));
        los.writeln("Message-Id: " +getHeader("Message-Id",null).toString());
        los.writeln("From: "+fromheader);
        los.writeln("Reply-To: "+replyto);
        los.writeln("To: "+getHeader("To",",").toString());
        System.out.println("From header is "+getHeader("From",",")+" mail from is "+mailfrom);
        //out.write(Message.RecipientType.TO, getAddress(email));
        los.writeln("subject: "+getHeader("Subject",null).toString());

        Enumeration hdrLines = getNonMatchingHeaderLines(ignoreList);
        while (hdrLines.hasMoreElements())
            los.writeln((String)hdrLines.nextElement());
        los.writeln();
    }catch(Exception e){
        System.out.println(e);
    }finally{
        try{
            if(los != null) los.flush();
        }catch(Exception e){
            System.out.println(e);
        }
    }
}

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

2 个答案:

答案 0 :(得分:0)

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

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
           //do something
        }

答案 1 :(得分:0)

您不必覆盖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>

在这种情况下,您可以访问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());
        }