开发者

Background Image positioning for MouseEnter (hover) event in Silverlight

开发者 https://www.devze.com 2023-02-10 22:33 出处:网络
On firing the MouseEnter event for TextBlock, nested inside the Border element, I want to clip a specific portion of an image (equivalent to CSS\' background-position: -1100px -24px) to place it as a

On firing the MouseEnter event for TextBlock, nested inside the Border element, I want to clip a specific portion of an image (equivalent to CSS' background-position: -1100px -24px) to place it as a background of Border element. Following is the code sofar:

XAML

<Border Margin="42,9,0,0">
    <TextBlock Style="{StaticResource MenuItem}" Name="metallicaButton" MouseEnter="metallicaButton_MouseEnter" Text="metallica" />
</Border>

C#

 private void metallicaButton_MouseEnter(object sender开发者_运维问答, MouseEventArgs e)
    {
        /*
           ImageBrush img = new ImageBrush();
           img.ImageSource = new BitmapImage(new Uri(@"Images/frame.png", UriKind.Relative)); ;
           img.Stretch = Stretch.None;            

           need to develop a subsitute for CSS code:
           span:hover{background-position: -1100px -24px;}

           ...something more versatile than:
           img.AlignmentX = AlignmentX.Center;
           img.AlignmentY = AlignmentY.Bottom;

           ((Border)metallicaButton.Parent).Background = img;

                         OR

           Rect r = Rect.Empty;
           r.X = -1100;
           r.Y = -24;
           RectangleGeometry g = new RectangleGeometry();
           g.Rect = r;
           Image src = new Image();
           src.Source = new BitmapImage(new Uri(@"Images/frame.png", UriKind.Relative)); ;
           src.Clip = g;

           is there a way to do this:
           ((Border)((TextBlock)sender).Parent).Background = src;
        */
    }

is there a way to do that?


Since you named your TextBlock "metallicaButton" anyway, is there any reason why don't you use an actual Button control and create this effect as a visual state in a custom button template using XAML?

0

精彩评论

暂无评论...
验证码 换一张
取 消