1

我正在开发window phone 7 应用程序。我是 windows phone 7 应用程序的新手。在我的应用程序中,我动态创建了按钮控件并将背景图像添加到按钮控件,如下所示

 Button AlphabetButton = new Button();
                AlphabetButton.Content = vAlphabet;
                ImageBrush brush = new ImageBrush();
                brush.ImageSource = new BitmapImage(new Uri("button_off.png", UriKind.Relative));
                //brush.Stretch = Stretch.None;
                AlphabetButton.Background = brush;
                AlphabetButton.BorderBrush = new SolidColorBrush(Colors.Gray);

                AlphabetButton.Margin = new Thickness(-12, -27, 0, 0);
                AlphabetButton.Width = 80;
                AlphabetButton.Height = 80;             

我可以看到按钮控件的背景图像,但它不适合按钮控件占用的空间。我可以看到按钮控件右侧和底部边框上的空白区域。这是因为图像不适合按钮控件占用的空间。我应该如何在背景中添加图像,使其适合按钮控件占用的空间。您能否提供我可以解决上述问题的任何代码或链接?

4

3 回答 3

1

将注释掉的代码行更改StretchFill

brush.Stretch = Stretch.Fill;
于 2011-02-08T11:49:39.117 回答
0

您可以在 xaml 中执行此操作。对于按钮内容,请使用网格而不是堆栈面板,否则您将获得未完全显示的图像

  <Button>
        <Button.Content>
            <Grid>
                <Image x:Name="buttonIcon"
                       Source="{Binding Image}"
                       Stretch="Fill">
                </Image>
            </Grid>
         </Button.Content>
   </Button>
于 2014-07-31T06:44:21.477 回答
0

难道这个空白是因为您将边距设置为-12。-27, 0, 0?

图像将填充按钮的区域 (80x80),但随后整个按钮向左移动 12 像素并向上移动 27 像素,这会在右侧和底部创建一些您未预料到的空白空间。

我怀疑您需要将图像添加到大小为 92、107 的按钮的内容中,以获得您想要的外观,尤其是在代码中非常混乱。

于 2011-02-08T13:03:34.280 回答