2

从后面的代码中,我可以为我的 groupbox 和 stackpanel 设置一些通用的东西,但是我找不到任何关于如何从后面的代码中突出显示堆栈面板的信息。

        GroupBox groupbox = new GroupBox(); 
        groupbox.Header = String.Format(node.Element("StudentID").Value); 
        groupbox.Width = 100; 
        groupbox.Height = 100; 
        groupbox.Margin = new Thickness(1); 

        TextBlock textBlock = new TextBlock(); 
        textBlock.Text = String.Format(node.Element("FirstName").Value + " " + (node.Element("LastName").Value)); 
        textBlock.TextAlignment = TextAlignment.Center; 

        TextBlock textBlock1 = new TextBlock(); 
        textBlock1.Text = (DateTime.Parse(node.Element("TimeAdded").Value)).ToString("d"); 
        String.Format("{0:d/M/yyyy}", DateTime.Parse(node.Element("TimeAdded").Value)); 
        textBlock1.TextAlignment = TextAlignment.Center; 
        textBlock1.VerticalAlignment = VerticalAlignment.Bottom; 

        StackPanel stackPanel = new StackPanel(); 
        stackPanel.Children.Add(groupbox); 

        stackPanel.Children.Add(textBlock); 
        stackPanel.Children.Add(textBlock1); 
        stackPanel.Margin = new Thickness(5);

我希望在鼠标悬停时创建一个浅灰色的突出显示,此代码也属于自定义控件。

4

1 回答 1

4

为 MouseEnter 和 MouseLeave 事件添加处理程序:

public MainWindow()
{
    InitializeComponent();

    StackPanel stackpanel = new StackPanel(); 
    stackpanel.MouseEnter += new MouseEventHandler(stackpanel_MouseEnter);
    stackpanel.MouseLeave += new MouseEventHandler(stackpanel_MouseLeave);
}

void stackpanel_MouseLeave(object sender, MouseEventArgs e)
{
    StackPanel stackpanel = (StackPanel)sender;
    stackpanel.Background = Brushes.Transparent;
}

void stackpanel_MouseEnter(object sender, MouseEventArgs e)
{
    StackPanel stackpanel = (StackPanel)sender;
    stackpanel.Background = Brushes.LightGray;
}
于 2012-04-17T03:52:24.467 回答