Silverlight 中 MouseEnter(悬停)事件的背景图像定位

发布于 2024-10-17 11:37:36 字数 1506 浏览 6 评论 0原文

在触发嵌套在 Border 元素内的 TextBlock 的 MouseEnter 事件时,我想剪切图像的特定部分(相当于 CSS 的背景位置:-1100px -24px)以将其放置为 Border 元素的背景。以下是到目前为止的代码:

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;
        */
    }

有办法做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅唱々樱花落 2024-10-24 11:37:36

既然您将 TextBlock 命名为“metalicaButton”,是否有任何理由不使用实际的 Button 控件并使用 XAML 在自定义按钮模板中创建此效果作为视觉状态?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文