C# Silverlight中鼠标悬停事件的背景图像定位
在为嵌套在Border元素中的TextBlock触发MouseEnter事件时,我想剪辑图像的特定部分(相当于CSS的背景位置:-1100px-24px),将其作为Border元素的背景。以下是迄今为止的代码: XAMLC# Silverlight中鼠标悬停事件的背景图像定位,c#,silverlight,xaml,image-clipping,C#,Silverlight,Xaml,Image Clipping,在为嵌套在Border元素中的TextBlock触发MouseEnter事件时,我想剪辑图像的特定部分(相当于CSS的背景位置:-1100px-24px),将其作为Border元素的背景。以下是迄今为止的代码: XAML <Border Margin="42,9,0,0"> <TextBlock Style="{StaticResource MenuItem}" Name="metallicaButton" MouseEnter="metallicaButton_Mou
<Border Margin="42,9,0,0">
<TextBlock Style="{StaticResource MenuItem}" Name="metallicaButton" MouseEnter="metallicaButton_MouseEnter" Text="metallica" />
</Border>
有没有办法做到这一点?既然您将文本块命名为“metallicaButton”,那么有没有理由不使用实际的按钮控件,并使用XAML在自定义按钮模板中创建此效果作为视觉状态?可能会有所帮助。我不想在那里重复。
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;
*/
}