Silverlight 中 MouseEnter(悬停)事件的背景图像定位
在触发嵌套在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
既然您将 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?