tags:

views:

328

answers:

3

If I create a button with an image inside, it gets blown up to much larger size than the image.

If i try to constrain the size of image, and container button, the image gets cut off:

<Button Background="Transparent" Width="18" Height="18" Margin="0,0,0,0" Padding="0,0,0,0" BorderBrush="{x:Null}">
    <Image Width="16" Height="16" />
</Button>

Native image size is 16x16. Here the button is 18x18 with 0 padding, yet the image still gets cut-off on right/bottom.

how can i make the entire button be exactly the size of the image w/out any cut off?

+1  A: 
gehho
i didn't experience any stretching. by default the image was in the middle, with a lot of space between the image and button borders. by default the button is a lot bigger than the containing image, and there is no stretching. image alignment is not the issue here
Sonic Soul
Did you check my first code sample? The second one was just an alternative. You should set the *Button's* alignment properties correctly; then the Button should size to its content, i.e. the image.
gehho
yes i have.. it makes no difference. same as default. my code: <Button Background="Transparent" Margin="0,0,0,0" Padding="0,0,0,0" BorderBrush="{x:Null}"> <Image HorizontalAlignment="Center" VerticalAlignment="Center" Source="Resources/expand.png" /> </Button>
Sonic Soul
BUTsetting horizontal alignment, and vertical alignemnt to center, AND setting image widht and button width to 16x16, brings me close!!the only thing left is a weird border slill showing on mouse over and when containing row is selected...
Sonic Soul
You need to set the alignment on the Button, not the Image. Read gehho's original answer again.
John Bowen
@Sonic Soul: Did you read my answer? Or just copied some code randomly?? Please read the paragraphs before the XAML! And choose the **first** code example, not the second. I think I will edit the answer to make it even more clear...
gehho
ok, i applied the properties to the button, not the image. But there is still no difference. Not until i set width/height of image and button. <Button Click="TestGridColumnButton_Click" Background="Transparent" HorizontalAlignment="Center" VerticalAlignment="Center" Margin="0,0,0,0" Padding="0,0,0,0" BorderBrush="{x:Null}"> <Image Source="Resources/expand.png" /> </Button>
Sonic Soul
Just checked the code in Blend. You need to set `HorizontalAlignment` and `VerticalAlignment` of the Button to `Center`, **and** set the `Image`'s `Stretch` property to `None` because this property defaults to `Uniform` which blows up the image as far as it can. See my edited code example in my original answer for a working example.
gehho
+1  A: 

The beauty (and curse?) of WPF is the ability to re-template controls.

You can set the template of the button something like the following (this is free hand, so you will need to tweak it a bit for your tastes):

        <Button x:Name="btn16x16">
            <Button.Template>
                <ControlTemplate>
                    <Border HorizontalAlignment="Center" VerticalAlignment="Center" >
                        <Image Source="pack://siteoforigin:,,,/Resources/SixteenBySixteen.png" 
                               Width="16" 
                               Height="16"/>
                    </Border>
                </ControlTemplate>
            </Button.Template>
        </Button>
Wonko the Sane
A: 

Think this might give people the answer they are looking for, buttons with image and no borders at all.

Steveo
isnt it the same as Wonko's suggestion? I guess if all else fails, messing with control template is needed for a image button in WPF
Sonic Soul