2014-11-08 104 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