在触发TextBlock的MouseEnter事件时,嵌套在Border元素中,我想剪辑图像的特定部分(相当于CSS'background-position:-1100px -24px),将其作为Border元素的背景。以下是代码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;
*/
}
有办法吗?
答案 0 :(得分:0)
由于您无论如何都将TextBlock命名为“metallicaButton”,是否有任何理由不使用实际的Button控件并使用XAML在自定义按钮模板中将此效果创建为可视状态?