how to set inner text margin of button - wpf

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" />

Related

Hide overflowing portions of textblock and image outside a wpf Button

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>

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.

TextBlock.TextWrapping - how to make the text wrap so that the lines are center-aligned?

In a Windows Phone 7 application, when I place a TextBlock in the grid and set its HorizontalAlignment to "Center" and its TextWrapping to "Wrap", why does the text that overflows the width of the container and is placed on the next line, align with the left side of the otherwise center-aligned block?
Is there any way to setup text wrapping so that all of the text in the text block is center-aligned?
You are probably missing TextAlignment:
<TextBlock TextAlignment="Center" />
HorizontalAlignment will center the TextBlock, TextAlignment will align the text inside the textblock.

Auto resizing wpf elements with scroll bars (Rich text box, list box) vb

I'm having a problem where I have elements such as Listboxes and Rich Text boxes that I want to set to size automatically in xaml according to the size of the window, but I only want it to resize to the size of the window and then put scrollbars if the content is any bigger than that.
Unfortunately, the only way I can get scroll bars to work is if I set a specific height of the listbox/rich text box (which does not work because I want it to automatically resize to the height of the grid that it is contained within, which is generally the height of the window (auto).
Any help would be greatly appreciated.
You do not need to use fixed values for Width and Height - you should rather specify a minimum width/height for your controls using the MinWidth and MinHeight properties. Then try a layout similar to this:
<Window>
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto">
<Grid>
<ListBox MinWidth="500" MinHeight="250"/>
<!-- any other controls... -->
</Grid>
</ScrollViewer>
</Window>
The Grid generally uses all the space it gets if its alignment properties are set to Stretch and if at least one row/column is set to be star-sized. In this case, there are only one row and one column, implicitly created, both star-sized by default.
To make the ScrollViewer work, you need to somehow set a minimum size of your content controls because otherwise the ScrollViewer does not know when to activate the ScrollBars. In the example above, I have done that using the MinHeight and MinWidth properties of the ListBox, but you could also set these properties on the Grid's RowDefinitions and/or ColumnDefinitions.
Now, if you resize the window, so that the Width becomes smaller than 500, you will see that scrollbars will appear. Just check it out.

WPF Window MinWidth depending on size of toolbar

Before asking this question I have looked at all related questions, but have not found anything relevant.
In my application I have toolbox style bar, which is basically stack panel with bunch of buttons. User may change which buttons are shown in toolbar.
Window width may be changed, but it can not be smaller then width of toolbar.
At first I was hoping to bind the MinWidth property to the StackPanel Width property and create converter that adds few pixels to Width of the StackPanel. The problem is that my converter does not get Width of StackPanel, just NaN as value :(
Unfortunately, StackPanel width is set to Auto and I can not change that.
Is there any way I can make my Window MinWidth dependable on Width of StackPanel?
Use ActualWidth, not Width.
Width is the requested width, or NaN for "Auto", ActualWidth is the, well, actual width after all the layout is calculated.

Resources