Silverlight 如何拉伸图像以适应所需的空间?
我正在开发WindowsPhone7应用程序。我不熟悉Windows phone 7应用程序。在我的应用程序中,我动态地创建了按钮控件&将背景图像添加到按钮控件中,如下所示Silverlight 如何拉伸图像以适应所需的空间?,silverlight,button,windows-phone-7,background-image,stretch,Silverlight,Button,Windows Phone 7,Background Image,Stretch,我正在开发WindowsPhone7应用程序。我不熟悉Windows phone 7应用程序。在我的应用程序中,我动态地创建了按钮控件&将背景图像添加到按钮控件中,如下所示 Button AlphabetButton = new Button(); AlphabetButton.Content = vAlphabet; ImageBrush brush = new ImageBrush(); bru
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;
我可以看到按钮控件的背景图像,但它不适合按钮控件占用的空间。我可以看到按钮控件右下边框上的空白。这是因为图像不适合按钮控件占用的空间。我应该如何在背景中添加图像,以使其适合按钮控件占用的空间。您能为我提供解决上述问题的代码或链接吗?更改注释掉的代码行,将
拉伸设置为填充:
brush.Stretch = Stretch.Fill;
将注释掉的代码行更改为将拉伸设置为填充
:
brush.Stretch = Stretch.Fill;
这可能是因为您将边距设置为-12-27,0,0
图像将填充按钮(80x80)的区域,但整个按钮向左移动12像素,向上移动27像素,这会在右侧和底部产生一些您意想不到的空白
我怀疑您需要在按钮的内容中添加一个大小为92、107的图像,以获得您想要的外观,尤其是在代码中,它非常混乱。可能是因为您将边距设置为-12,所以出现了此空白-27,0,0
图像将填充按钮(80x80)的区域,但整个按钮向左移动12像素,向上移动27像素,这会在右侧和底部产生一些您意想不到的空白
我怀疑您需要在按钮的内容中添加一个大小为92、107的图像,以获得您想要的外观,尤其是在代码中,它相当混乱。您可以在xaml中执行此操作。对于按钮内容,请使用网格而不是stackpanel,否则将显示一个未完全显示的图像
<Button>
<Button.Content>
<Grid>
<Image x:Name="buttonIcon"
Source="{Binding Image}"
Stretch="Fill">
</Image>
</Grid>
</Button.Content>
</Button>
您可以在xaml中执行此操作。对于按钮内容,请使用网格而不是stackpanel,否则将显示一个未完全显示的图像
<Button>
<Button.Content>
<Grid>
<Image x:Name="buttonIcon"
Source="{Binding Image}"
Stretch="Fill">
</Image>
</Grid>
</Button.Content>
</Button>
Stretch.Fill,Stretch.Uniform,Stretch.Uniform或mtofill,Stretch.None所有这些都不工作Stretch.Fill,Stretch.Uniform,Stretch.Uniform,Stretch.None所有这些都不工作