2014-11-08 110 views
1

上按一下按鈕標準藍色背景我使用下面的代碼顯示了許多按鈕在我的滾動型刪除Windows Phone的8 C#

foreach (RingModel ring in app.rings) 
      { 
       Button btn = new Button(); 
       btn.BorderThickness = new Thickness(0, 0, 0, 0); 

       Image img = new Image(); 
       img.Width = 100; 
       img.Height = 100; 
       img.Margin = new Thickness(5, 0, 0, 0); 

       Uri myUri = new Uri(ring.ringThumbNailImagePath, UriKind.Absolute); 
       BitmapImage bmi = new BitmapImage(); 
       bmi.CreateOptions = BitmapCreateOptions.None; 
       bmi.UriSource = myUri; 
       img.Source = bmi; 
       btn.Content = img; 
       btn.Tag = i; 
       btn.Click += Ring_Click; 

       scrollStackPanel.Children.Add(btn); 
       i++; 
      } 

然而這些按鈕是給上點擊藍色的背景。我想讓它透明。我怎樣才能使它成爲可能。

回答

3

對您分享的App.xaml的<Application.Resources>

寫這個

<SolidColorBrush x:Key="ButtonDisabledBackgroundThemeBrush" Color="Ur color"/> 
    <SolidColorBrush x:Key="ButtonPressedBackgroundThemeBrush" Color="Ur Color" /> 

欲瞭解更多 http://msdn.microsoft.com/en-us/library/windows/apps/xaml/jj709909.aspx

+0

完美,鏈接很棒 – 2014-11-25 15:10:19

1

使用自定義樣式:

<Style x:Key="ButtonStyle1" TargetType="ButtonBase"> 
    <Setter Property="Background" Value="Transparent"/> 
    <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> 
    <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
    <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
    <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> 
    <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/> 
    <Setter Property="Padding" Value="10,5,10,6"/> 

    <Setter Property="Template"> 
    <Setter.Value> 
     <ControlTemplate TargetType="ButtonBase"> 
     <Grid Background="Transparent"> 
      <VisualStateManager.VisualStateGroups> 
      <VisualStateGroup x:Name="CommonStates"> 
       <VisualState x:Name="Normal"/> 
       <VisualState x:Name="MouseOver"/> 
       <VisualState x:Name="Pressed"> 
       <Storyboard> 
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Foreground"> 
        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}" /> 
        </ObjectAnimationUsingKeyFrames> 
       </Storyboard> 
       </VisualState> 
       <VisualState x:Name="Disabled"> 
       <Storyboard> 
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentContainer" Storyboard.TargetProperty="Foreground"> 
        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}" /> 
        </ObjectAnimationUsingKeyFrames> 
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="BorderBrush"> 
        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}" /> 
        </ObjectAnimationUsingKeyFrames> 
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ButtonBackground" Storyboard.TargetProperty="Background"> 
        <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent" /> 
        </ObjectAnimationUsingKeyFrames> 
       </Storyboard> 
       </VisualState> 
      </VisualStateGroup> 
      </VisualStateManager.VisualStateGroups> 
      <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" CornerRadius="0" Background="{TemplateBinding Background}" Margin="{StaticResource PhoneTouchTargetOverhang}" > 
      <ContentControl x:Name="ContentContainer" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}" Padding="{TemplateBinding Padding}" Content="{TemplateBinding Content}" ContentTemplate="{TemplateBinding ContentTemplate}"/> 
      </Border> 
     </Grid> 
     </ControlTemplate> 
    </Setter.Value> 
    </Setter> 
</Style> 

然後分配樣式的按鈕:

btn.Style = (Style) Resources["ButtonStyle1"]; 
+0

請你詳細說明如何創建一個新的風格,因爲這是我的第一個Windows應用程序。這將是一個很大的幫助。問候 – 2014-11-08 07:52:54

+0

@MuhammadUmar只需按Ctrl + C/Ctrl + V將上面的樣式放置到您的頁面資源屬性中,如下所示: {PASTE STYLE HERE}然後使用上面的行分配樣式。 – 2014-11-08 08:10:33

+0

謝謝,它支持windowsphone8 xaml – 2015-04-13 11:09:48