1

Border 要素内にネストされた TextBlock の MouseEnter イベントを発生させると、画像の特定の部分 (CSS の background-position: -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」という名前を付けたので、実際のボタンコントロールを使用して、XAMLを使用してカスタムボタンテンプレートでこの効果を視覚的な状態として作成しない理由はありますか?

于 2011-02-16T18:06:14.007 に答える