1

在触发嵌套在 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;
        */
    }

有没有办法做到这一点?

4

1 回答 1

0

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

于 2011-02-16T18:06:14.007 回答