2012-04-17 41 views
2

從後面的代碼我可以設置一些通用的東西到我的groupbox和stackpanel,但我無法找到任何關於如何突出顯示代碼背後的堆棧面板。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); 

我希望在鼠標上創建一個灰色的亮點,此代碼也屬於自定義控件。

+0

掛鉤的鼠標事件,改變背景顏色? – 2012-04-17 03:27:29

+1

是否有你在代碼隱藏而不是在XAML中這樣做的原因? – 2012-04-17 03:53:02

回答

4

添加的處理程序的MouseEnter和鼠標離開事件:

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

非常好的答案正是我需要的。 – 2012-04-17 05:09:40

相關問題