1

我正在开发window phone 7应用程序。我不熟悉window 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;    

我可以看到背景图片的按钮控制,但它不适合通过按钮控制所占用的空间。我可以看到右边的&按钮控件底部边框上的空白区域。这是因为图像不适合按钮控制占用的空间。我应该如何在后台添加图像,以便它能够适应按钮控件所占用的空间。你能否给我提供任何可以解决上述问题的代码或链接?

回答

0

难道说这个空白是因为您已设置的保证金-12。 -27,0,0?

图像将填满按钮区域(80x80),但整个按钮左移12像素,最多增加27像素,这会在右侧和底部创建一些您并不期待的空白区域。

我怀疑你需要添加一个图像到92的大小的按钮的内容,107得到你以后的外观,特别是在代码中很麻烦。

1

更改代码的注释行设置StretchFill

brush.Stretch = Stretch.Fill;
+1

Stretch.Fill,Stretch.Uniform,Stretch.UniformToFill,Stretch.None所有这些不工作 – 2011-02-08 11:56:07

0

您可以在xaml中执行此操作。而对于按钮的内容使用电网的,而不是其他的StackPanel你有没有完全显示的图像

<Button> 
     <Button.Content> 
      <Grid> 
       <Image x:Name="buttonIcon" 
         Source="{Binding Image}" 
         Stretch="Fill"> 
       </Image> 
      </Grid> 
     </Button.Content> 
    </Button>