簡體   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>

請勿設置按鈕的content屬性。 也給按鈕適當的大小,否則圖像控件將使其過大

將控件放在堆棧面板中,該面板可以容納多個孩子,而不是按鈕支持的僅一個孩子,並且您可以更輕松地控制大小。


根據您提供的圖像,這是我的代碼。 另外,您應該在您的問題中添加該圖片鏈接

<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>

根據需要更改按鈕大小。 另外,您可能需要使用VisualStateManager更改其他視覺狀態的背景,但結果會更整潔。 也有非常骯臟的把戲。 我沒有為此發布代碼。 使用畫布作為內容,設置背景,然后在其中插入文本塊。 我不知道這項工作會做,但是我還是不會做。 建議使用VisualStateManager。

圖像用作按鈕的最簡潔方法是設置樣式屬性。

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

通過定義新的塊<Grid.Resources></Grid.Resources>在頁面中設置此代碼,或者可以在StandardStyles.xaml通用地進行此操作,並且在定義新的Button時,將BasedOn屬性設置為Key值您在樣式中定義的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM