6

As I see, WP7 buttons are surrounded by a black space of some pixels. How I can remove it?

I've read this post, but I didn't managed to remove it! Could you please give a working example?

I've tried to wrap button with <Border... /> element, but it didn't work.

this is my XAML

<ItemsControl Name="icCells" Grid.Row="1">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <Button Background="Red" BorderThickness="0"  Width="40" Height="40" BorderBrush="Green" Padding="0" Margin="0" />

                </DataTemplate>                   
            </ItemsControl.ItemTemplate>
            <ItemsControl.ItemsPanel>
                <ItemsPanelTemplate>
                    <toolkit:WrapPanel/>
                </ItemsPanelTemplate>
            </ItemsControl.ItemsPanel>
        </ItemsControl>

Thanks

Community
  • 1
  • 1
theateist
  • 13,879
  • 17
  • 69
  • 109
  • Possibly the same question as http://stackoverflow.com/questions/4448293/windows-phone-7-button-borders – mostruash Jan 23 '12 at 02:14

3 Answers3

14

Try the following:

Style

<phone:PhoneApplicationPage.Resources>
    <Style x:Key="CustomButtonStyle" TargetType="Button">
        <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 PhoneFontSizeMediumLarge}"/>
        <Setter Property="Padding" Value="0"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Grid Background="Transparent">
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="CommonStates">
                                <VisualState x:Name="Normal"/>
                                <VisualState x:Name="MouseOver"/>
                                <VisualState x:Name="Pressed">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneBackgroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneForegroundBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="Disabled">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/>
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/>
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="0">
                            <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</phone:PhoneApplicationPage.Resources>

XAML

<Button Style="{StaticResource CustomButtonStyle}"/>

If you want to change the Margin (I made it 0) search for the following in the Style:

Margin="0"

If you want to change the Padding (I made it 0) search for the following in the Style:

"Padding" Value="0"

I do recommend change it a little smaller...I personally think its a little large to much padding

MyKuLLSKI
  • 5,285
  • 3
  • 20
  • 39
5

You could do that easily in Blend.

Open the project in blend. Right click button and from the menu select Edit Template-> Edit a copy -> Name the style.

On the right side, in the object and timeline window, You can see a grid inside which you have a button background and a contentcontainer. Resize the container to the size of the button, now you wud see no white space. Now this same style can be applied to other buttons too.

alfah
  • 2,077
  • 1
  • 31
  • 55
  • 1
    AweSome Answer, Its really so easy via Blend, Good job Mr. Alfah. Keep it up...! I can only up ur vote once, otherwise I would up it to 100. Great...! – Zia Ur Rahman Dec 05 '13 at 11:01
0

You can find some theory here: http://msdn.microsoft.com/en-us/library/ms745683.aspx

Maxim Kamalov
  • 745
  • 9
  • 23