Hide overflowing portions of textblock and image outside a wpf Button - wpf

I have a WPF button.The ContentControl inside this Button contains other UI elements like Textblock,Images etc.When i increase the font size of the Textblock or the Image etc,or i change the text inside Textblock,then some portion of the text and image can sometimes be seen overflowing outside the button's(ie parent control) height and width if the text ,fontsize or image size is more than the height and width of this button.I want to hide these overflowing portions of the text and image controls,ie which only the portions of the Textblock and Image which lie outside the button contentcontrol's height and width boundary.How can i do this?

I was able to do it.
I simply placed a the entire ContentControl inside a Label and it was done.
ie earlier it was like this:-
<Button>
<ContentControl>
<TextBlock/>
</ContentControl>
</Button>
Now i made it like this:-
<Button>
<Label>
<ContentControl>
<TextBlock1/>
<TextBock2/>
.
.
.
<Image1/>
<Image2/>
</ContentControl>
</Label>
</Button>

Related

How to create border for png image

I have a custom image in .png format. I want to create button. The form of button is this image. So I put the image inside button template and want it be fleshed only when mouse over the image. The shape of image is rectangle (http://prntscr.com/lz5m5q), but in fact it is custom shape. So how can I set border for button and ignore transparent background of image (I don't want see border as rectangle, border should be around my actual image).
hope i got correctly, is this you want ?
Style Link
due to characters limit i am posting style in above link. copy paste codes from above link in Windows.Resources or where you want.
<Window.Resources>
<!-- I put this in windows resources you can put it app.xaml or any resource dictionary -->
<!-- Copy here content from the above link link because of stackoverflow character limit i cant paste it here. -->
</Window.Resources>
<Grid>
<Button Height="250" Width="300" Style="{DynamicResource CountryButtonStyle}" />
</Grid>
Output:

WPF - Don't stretch image but fit it in a container (or MaxHeight)

I would like to display an image in WPF in its original size, which can be achieved by setting Stretch="None". However, if the image is bigger than its enclosing element, the image gets cropped, which is undesirable.
I would like the image to be downsized if it doesn't fit, but not stretched when it is smaller than its enclosing element. Is there a way to achieve this behaviour?
Set the Image control's StretchDirection property:
<Image Stretch="Uniform" StretchDirection="DownOnly" ... />

Image to not cause a row size change

I have a simple Grid with three columns. In one of these rows, I have (in column order): A TextBlock, a TextBox, and a Button. The Button contains an Image for it's content.
My issue is that the Image always sizes to display it's full content which enlarges the button. I don't care what size the image's source is; I want the Button to be the same size as the row already is due to the TextBlock and the TextBox. I don't want to specify a hard coded value for the image size. That's not the point. If a user's theme and/or font changes, the Grid row should still work as expected and the Button should always be the correct size.
I've tried a ViewBox, all sorts of alignment properties and bindings. No luck. Any ideas anyone?
<TextBlock VerticalAlignment="Center" Margin="4,0" x:Uid="RONumber" />
<TextBox x:Name="tbRO" InputScope="Number" MaxLength="8" Grid.Column="1" Margin="4" />
<Button x:Name="btnSearch" Click="Search_Click" Grid.Column="2">
<Image Source="Long Source Removed For Readability" />
</Button>
In the image below you can see the TextBox and to the right the Button with the Image inside it. Right below it is another TextBox which is correctly sized. And if I remove the button the top TextBox sizes the same.
Ok, so here's the answer. Because the elements are in the same row the XAML is not working. The binding I had made (before I even posted this question) to the ActualHeight always was 0. I instead bound the MaxHeight of the button to the ActualHeight of a random TextBox on another area of the screen. This worked.

how to set inner text margin of button

I have a WPF button with some text in it. If I don't set button's width manually, then the button width will be almost of text's width, and it doesn't look very good. I don't want to calculate the width of each button. Is there a more convenient way to specify the relative width of the button? For example in CSS you can specify padding or margin. Is there something like that in WPF?
You can set the Button's Padding
<Button Content="Test" Padding="5,2" />

How can i make a control sticky?

How can i make a control to be sticky? I have a scrollviewer and inside that a grid. Inside grid i have a button. The button is set to horizontal alignment - center and vertical alignment - bottom. Now, when the size of the grid increases the button is no more visible on the screen. I have to scroll to really see the button. Can i make the button to be sticky so that it is always there at the bottom of the screen?
Thanks in advance :)
Anything you put inside the scollviewer will be part of the scrollable content. You can either remove that control and put it outside the scrollviewer, or put inside a floating popup control.
Remove the button from inside the ScrollViewer:
<Grid>
<ScrollViewer>
//Content
</ScrollViewer>
<Button HorizontalAlignment="Center" VerticalAlignment="Bottom" />
</Grid>

Resources