简体   繁体   English

如何为Windows Phone 7的按钮设置图像内容

[英]How to set image content for a button for windows phone 7

我正在尝试为按钮设置图像,我的图像中不包含文本,我想手动为该图像设置文本,我尝试通过这种方式将按钮包含图像的按钮内容,但是我不知道如何为按钮设置文本它,我正在为Windows Mobile 7在Visual Studio 2012中工作

<Button x:Name="ButtonName" Height="Auto" Width="Auto">
    <StackPanel>
          <Image Source=image.JPG" Stretch="Fill" Height="Auto" Width="Auto" />
          <TextBlock Text="picture Button" TextAlignment="Center" />
    </StackPanel>
</Button>

Thing is donot set the content property of the button. 请勿设置按钮的content属性。 Also give appropriate size to the button otherwise image control will make it excessively big 也给按钮适当的大小,否则图像控件将使其过大

Put your controls inside the stackpanel which can take many children instead of only one child as supported by button, and you can control the size more easily. 将控件放在堆栈面板中,该面板可以容纳多个孩子,而不是按钮支持的仅一个孩子,并且您可以更轻松地控制大小。


Based on the image you gave, here is my code. 根据您提供的图像,这是我的代码。 Also you should put that image link in your question 另外,您应该在您的问题中添加该图片链接

<Button  Content="Text" Height="Auto" Width="Auto" BorderThickness="0">
     <Button.Background>
          <LinearGradientBrush StartPoint="0 0" EndPoint="0 1">
               <LinearGradientBrush.GradientStops>
                   <GradientStopCollection>
                       <GradientStop Offset="0" Color="#FF03CCF0" />
                       <GradientStop Offset="1" Color="#FF0182C2" />
                   </GradientStopCollection>
               </LinearGradientBrush.GradientStops>
           </LinearGradientBrush>
      </Button.Background>
  </Button>

Change button size according to requirement. 根据需要更改按钮大小。 Also you may need to change the background for other visual states using VisualStateManager, but the result will be much cleaner. 另外,您可能需要使用VisualStateManager更改其他视觉状态的背景,但结果会更整洁。 Also there is very dirty trick too. 也有非常肮脏的把戏。 I am not posting code for it. 我没有为此发布代码。 use a canvas as content, set it's background and then insert a textblock inside. 使用画布作为内容,设置背景,然后在其中插入文本块。 I don't know will this work, but I wont do it anyway. 我不知道这项工作会做,但是我还是不会做。 using VisualStateManager is recommended. 建议使用VisualStateManager。

The cleanest way for an Image to act as a Button is to set the style property. 图像用作按钮的最简洁方法是设置样式属性。

<Style x:Key="CustomButton" TargetType="Button">
    <Setter Property="Template" >
        <Setter.Value >
            <ControlTemplate>
                <Image Source="Images/Sample.jpg" />
            </ControlTemplate >
        </Setter.Value >
    </Setter >

Set this code in your page by defining a new block <Grid.Resources></Grid.Resources> or you could do it universally in the StandardStyles.xaml and when you're defining a new Button set the BasedOn property to the Key value that you've defined in the style. 通过定义新的块<Grid.Resources></Grid.Resources>在页面中设置此代码,或者可以在StandardStyles.xaml通用地进行此操作,并且在定义新的Button时,将BasedOn属性设置为Key值您在样式中定义的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM