2013-05-03 113 views
0

我試圖在將鼠標懸停在標籤上時更改標籤文本的顏色。我試圖把這個命令放在previewmousemove事件中,但這不起作用。將鼠標懸停在標籤上時更改標籤的前部顏色

private void hand_PreviewMouseMove(object sender, PreviewMouseEventArgs e) 
     { 
      Cursor.Current = Cursors.Hand; 
      xrLabel260.BackColor = Color.CornflowerBlue; 
     } 

之後,這並沒有工作,我試圖用鼠標事件/ mouseleave事件來改變顏色。

private void xrLabel260_MouseEnter(object sender, EventArgs e) 
    { 

     xrLabel260.ForeColor = Color.CornflowerBlue; 

    } 

    private void xrLabel260_MouseLeave(object sender, EventArgs e) 
    { 
     xrLabel260.ForeColor = Color.Black; 

    } 

這也沒有工作。我怎樣才能改變我的代碼,以便它可以工作?預先感謝您的幫助。

+0

事件發生了嗎? – 2013-05-03 15:43:00

+0

previewmousemove正在發射,但似乎沒有辦法在devexpress設計器中使用mouseenter和mouseleave事件。 – 2013-05-03 15:46:11

回答

1

就我個人而言,我會在你的xaml中做這樣的事情:編輯:我修改了這個以顯示它如何適合主窗口。

<Window x:Class="WpfApplication1.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="MainWindow" Height="350" Width="525"> 
<Grid> 
    <Label Content="My Label"> 
     <Label.Style> 
      <Style TargetType="Label"> 
       <Setter Property="Foreground" Value="Black"/> 
       <Style.Triggers> 
        <Trigger Property="IsMouseOver" Value="True"> 
         <Setter Property="Foreground" Value="Blue"/> 
        </Trigger> 
       </Style.Triggers> 
      </Style> 
     </Label.Style> 
    </Label> 
</Grid> 

+0

我是這個程序的新手。這會在MainWindow.xaml中進行嗎? – 2013-05-03 15:50:34

+1

看看我的編輯。如果這仍然不清楚,請將您的代碼發給我,我會幫助您。我的電子郵件在我的檔案中。 – 2013-05-03 17:02:27

1

好像你沒有爲它添加事件處理程序(註冊標籤的鼠標事件):

xrLabel260.MouseEnter += xrLabel260_MouseEnter; 

最符合邏輯的地方,做到這一點是在形式上的負載程序。

編輯:爲WPF,你可以有這樣的事情在XAML(問題有EventArgs的,而不是MouseEventArgs,我認爲這是對的WinForms):

<Label x:Name="xrLabel260" Content="Label" MouseEnter="xrLabel260_MouseEnter"/> 

...然後在後臺代碼:

private void xrLabel260_MouseEnter(object sender, MouseEventArgs e) 
    { 
     xrLabel260.Foreground = Brushes.BlanchedAlmond; 
    } 
+0

感謝您的回覆。我正在使用WPF而不是winforms。我會在哪裏放這個? – 2013-05-03 16:06:22

相關問題