2012-05-02 30 views
2

在顯示在鼠標上的按鈕,我需要在只有silverlight.Please誰能幫助解決這個issue.Below是我的代碼重量我迄今所做的,顯示在鼠標上的按鈕,如何在Silverlight

<StackPanel x:Name="spDeleteContent" VerticalAlignment="Center" Margin="10,0,0,0" Width="20" Height="20" HorizontalAlignment="Center" Orientation="Vertical"> 
<Button x:Name="btnDeleteContent" Height="20" Width="20" Click="btnDeleteContent_Click"  BorderThickness="0" Visibility="Collapsed"> 
    <Image Source="/Assets/Images/close.png" Height="10" Width="10" Cursor="Hand" Margin="0"/> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="MouseEnter"> 
      <ei:ChangePropertyAction PropertyName="Visibility" Value="Visible"></ei:ChangePropertyAction> 
     </i:EventTrigger> 
     <i:EventTrigger EventName="MouseLeave"> 
      <ei:ChangePropertyAction PropertyName="Visibility" Value="Collapsed"></ei:ChangePropertyAction> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</Button> 
</StackPanel> 
+0

你的代碼在哪裏? – lionheart

+0

@ apoc29 ..請找到我更新的代碼... – Arun

回答

2

ü可以寫適當的行爲,例如:

namespace SilverlightApplication1.Helpers.Behaviors 
{ 
    public class MouseOverOpacity : System.Windows.Interactivity.Behavior<UIElement> 
    { 
    protected override void OnAttached() 
    { 
     AssociatedObject.Opacity = 0; 

     AssociatedObject.MouseEnter += AssociatedObjectOnMouseEnter; 
     AssociatedObject.MouseLeave += AssociatedObjectOnMouseLeave; 
    } 

    protected override void OnDetaching() 
    { 
     AssociatedObject.Opacity = 1; 

     AssociatedObject.MouseEnter -= AssociatedObjectOnMouseEnter; 
     AssociatedObject.MouseLeave -= AssociatedObjectOnMouseLeave; 
    } 

    private void AssociatedObjectOnMouseLeave(object sender, MouseEventArgs mouseEventArgs) 
    { 
     AssociatedObject.Opacity = 0; 
    } 

    private void AssociatedObjectOnMouseEnter(object sender, MouseEventArgs mouseEventArgs) 
    { 
     AssociatedObject.Opacity = 1.0; 
    } 
} 

}

XAML:

<Border BorderThickness="2" BorderBrush="Red" Height="100" Width="100"> 
     <Button Content="Button"> 
      <i:Interaction.Behaviors> 
       <behaviors:MouseOverOpacity /> 
      </i:Interaction.Behaviors> 
     </Button> 
</Border> 

您應該使用可見性屬性作爲方法,因爲當UIElement.Visibility = Collapsed時,它不會收到事件的通知。

4

您需要將代碼更改爲以下內容。

<StackPanel x:Name="spDeleteContent"> 
    <Button x:Name="btnDeleteContent" Visibility="Collapsed"> 
    <Image Source="/Assets/Images/close.png" Height="10" Width="10" 
      Cursor="Hand" Margin="0"/> 
    </Button> 
    <i:Interaction.Triggers> 
    <i:EventTrigger EventName="MouseEnter"> 
    <ei:ChangePropertyAction TargetName="btnDeleteContent" PropertyName="Visibility" 
           Value="Visible"></ei:ChangePropertyAction> 
    </i:EventTrigger> 
    <i:EventTrigger EventName="MouseLeave"> 
     <ei:ChangePropertyAction TargetName="btnDeleteContent" PropertyName="Visibility" 
           Value="Collapsed"></ei:ChangePropertyAction> 
    </i:EventTrigger> 
    </i:Interaction.Triggers> 
</StackPanel> 

MouseEnterMouseLeave添加EventTriggers到StackPanel中的觸發器和TargetName添加一個按鈕的名稱爲ChangePropertyAction