Button height in fraction - wpf

Is it possible to have button size in fractions?
I want the height to be specified as 23.7, but when I run the app and check the actual height, it is rounded to 24. The height and width are of type double so I expected it to take and honor the fractional values, but for some reason it is rounding up.
Is it possible to have fractional height with some settings/hack?
<Button
Grid.Column="0"
Margin="0,12,0,0"
Width="80"
Height="23.7"
Content="Market"
Command="{Binding OrderSellMarketButtonCommand}"/>
I even tried binding the Height to VM property of type double. That did not help either.
Screenshot of the issue:

The double type is slightly misleading in that case, because the actual size of the control depends on the currently set DPI value. Normally, at 100% the DPI value is 96 and since the default WPF measuring unit is a device independent pixel (which is 1/96 inch) it maps to whole pixels which aren't fractions.
It's a bit strange that you want to align something in the sub-pixel range, can you maybe show us an example or screenshot?
Edit: After seeing you screenshot, it looks like that the text in the middle has a slightly larger fontsize than the buttons. If it's acceptable, you can up the fontsize on the button by one:
<Button
Grid.Column="0"
Margin="0,12,0,0"
Width="80"
Height="23"
Command="{Binding OrderSellMarketButtonCommand}">
<Button.Content>
<TextBlock Text="Market" FontSize="13"/>
</Button.Content>
</Button>

Layout is complex in WPF. i put here a translation of a training slide.
Since WPF is completely independent of resolution, the prerequisite is never to specify sizes or coordinates so that the layout of the elements works regardless of the resolution or size of the screen.
The layout management process is based on the following principle:
That each element (a potential container) intervenes
That every container, for all these children
Demands their "desired" size
Then arranges them according to his "model" of organization
These 2 main methods are available in the Panel and are overloadable when creating custom controls and containers
MeasureOverride
ArrangeOverride
This means you will have no chance to get the values you put in Width and Height into the real ActualWidth and ActualHeight - Or you have a complete fixed layout but that will not fit with "independent of resolution"

Related

WPF control origin point

After some investigation, I still can't find method to change origin of control.
So, I want just to place one square exactly in center of another square, without margins, so it will be completely independent of first square size.
Theoretically, it can be easily done with HorizontalAlignment and VerticalAlignment set to Center, since it automatically sets Margin of control to half of width and height of parent control. But it is not so simple.
Simplest way to describe problem is next picture
As you can see, margin is counted towards upper left corner. Which is what I call origin. The perfect solution is to change it to center of first square, but this is where I need help - how can I do that?
Point of origin applies when using a transform object, and attaching the transform to your control. It won't actually effect the behaviour of the margin or left, top properties. If using a transform to place your object, point of origin is very useful.
The top, left (if using cavas) and margin (if using say grid) help govern the "auto" placement by the parent control, and this in turn governs where point of origin for the control winds up being relative to the parent control. The transform object then offsets RELATIVE to where that point of origin is.
The other useful thing is that transform overrides the auto placement in the parent control, or rather, forces an offset to where the parent wants to put it, which in some cases is useful - i.e., you might have boxes listed in a grid and want them to "shake" left and right when you hover the mouse over them, their alignment stays in order to the grid, but the transform lets you bump them away from their "forced" position.
For example, attach the same transform object to 2 controls, and set their origins separate, then apply an animation to the transform object - both controls will animate off the one animation object (if you wanted to their movement in perfect sync).
Well, it was weird enough. The given behaviour can be seen only when using Image, and Center alignment. Can be solved by either wrapping Image in Grid, which will be using Center alignment, or using Stretch alignment with Image (which is much simplier).
<Grid Width="500" Height="500">
<Image Width="250" Height="250" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/>
</Grid>
If you want to reproduce problem I've described in question, replace Stretch with Center in code above.
Probably oversimplifying here but I would just use a Grid to wrap the two items you mentioned like this example (One stretched to fit and one centered):
<Grid>
<Border HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderBrush="Black" BorderThickness="10" Margin="4"/>
<Button HorizontalAlignment="Center" VerticalAlignment="Center" Padding="10">InnerButton</Button>
</Grid>

Usercontrol, resize inside the grid

I have a typical user control with some drawing on the canvas.I have plugged that user control in a grid with following code
<Routine:FlashUserControl x:Name="FlashControl" Grid.Row="0" Grid.Column="2" Grid.RowSpan="9" Grid.ColumnSpan="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch" />,
but the user control overflow above the grid region (as specified in the attached image) rather than fitting into the region (2 Coloumns x 9 rows)
Even Though VerticalAlignment, HorizontalAlignment, HorizontalContentAlignment and VerticalContentAlignment defined as stretch in user control, why it is happening?
UPDATE:
If I summarize the issue, my drawing entities inside the usercontrol exceeds the canvas limits, I think I need to re-scale the drawings
There can only be two reasons why a UI element that has been set into a particular section of a Grid might exceed its bounds. The fist reason might be that as mentioned in the comments, the UI element has had its Width and/or Height property values explicitly set to values that are too big for its available area. The fix for this is obviously to not explicitly set these values.
The second reason might simply be that you Grid has not been declared correctly. Perhaps you have the wrong number of rows and/or columns? Again, the fix for this is simple. Please show your Grid so that we can verify this point.

Rotate the text inside a small grid/panel

<Label Grid.Row="1"
Height="70"
Margin="2"
Width="300"
Content="{l:Translate Key={x:Static l:MultistringTags.SHOW_MENU}}"
DockPanel.Dock="Bottom"
FontSize="20"
FontWeight="Bold"
Foreground="White">
<Label.RenderTransform>
<RotateTransform Angle="270" />
</Label.RenderTransform>
</Label>
Here I want to rotate the text, which is inside a grid and a grid column width equals with text height. In this case I see only part of text like if text was drawn without rotating cutted of by grid width, and rotated to required angle. I have tried panels, they give me same result.
Does anybody know some workaround to make it show all the text and I don't want to use image because text should be translatable.
I believe if you change it to set the LayoutTransform instead of RenderTransform, it will prevent the text from being cut off.
<Label.LayoutTransform>
<RotateTransform Angle="270" />
</Label.LayoutTransform>
Further to #nekizalb's answer stating that you should use a LayoutTransform, the reason that you would need to do that instead of using a RenderTransform is because of the timing at which each occurs... a LayoutTransform will affect results of layout.
From the UIElement.RenderTransform Property page on MSDN:
A render transform does not regenerate layout size or render size information. Render transforms are typically intended for animating or applying a temporary effect to an element. For example, the element might zoom when focused or moused over, or might jitter on load to draw the eye to that part of the user interface (UI).
From the FrameworkElement.LayoutTransform Property page on MSDN:
In contrast to RenderTransform, LayoutTransform will affect results of layout.
Example scenarios where LayoutTransform would be useful include: rotating elements such as menu components from horizontal to vertical or vice versa, scaling elements (zooming in) on focus, providing editing behavior, etc.

WPF item panel that keeps aspect ratio

I have an ItemsControl which may contain an arbitrary number of items (unknown at design time). Each of these items is basically represented as an infinitely-scalable image with a fixed aspect ratio (ie. the image will draw itself in whatever space is given to it -- it does not dictate its own size except that the aspect ratio must be preserved). The aspect ratio for each item might differ but is usually the same.
I want to:
Draw a border around each image, ideally of uniform thickness regardless of image scaling.
Draw each image as large as possible within the window, while maintaining its order, aspect ratio, and margins external to the border.
At least two of the four sides of an image must always touch the invisible boundaries of the cell it is within. The other two sides should be centred if not touching. (Assuming that some sort of uniform cell layout is used.)
Not overlap or clip any images.
Automatically re-layout as the containing window is resized.
Waste the minimum amount of non-image space.
Cope as well as possible if the aspect ratios of the items differ. (But it's ok if this increases the wasted space of other items, as long as they rescale to fit the result.)
The general consensus that I've found seems to be to wrap each image in a Stretch.Uniform Viewbox, and then put those into a UniformGrid. I've tried that approach but it doesn't appeal to me because:
Given two items, the UniformGrid always wants to create a 2x2 grid layout even when the window shape would make a 2x1 or 1x2 grid more suitable, which results in over-scaling and wasted space.
If I put the Border within the Viewbox then it scales the border thickness. If I put it outside then it distorts the aspect ratio.
Is there a better way to do this? (Note that the "image" is actually my own custom-draw FrameworkElement, so I can put custom measure/arrange code in here or in a custom container panel if it will help.)
<UniformGrid>
<Rectangle Fill="Red" Margin="4" Width="500" Height="281.25" />
<Rectangle Fill="Blue" Margin="4" Width="500" Height="281.25" />
</UniformGrid>
Here's a simple example. Put this into a window, then try resizing the window. The rectangles change size to fit the window (good), but they also change shape/aspect (bad), and they stop resizing once the window is sufficiently large (bad). Also it leaves space for an entire 2x2 grid even when the window itself is sized such that 1x2 or 2x1 would work better.
<UniformGrid>
<Viewbox Stretch="Uniform" Margin="4"><Rectangle Fill="Red" Width="500" Height="281.25" /></Viewbox>
<Viewbox Stretch="Uniform" Margin="4"><Rectangle Fill="Blue" Width="500" Height="281.25" /></Viewbox>
</UniformGrid>
This works marginally better in that the rectangles continue to stretch when the window gets large, and they no longer distort their shape, but there's still the needless second row when the window is wide or second column when the window is narrow. And I would prefer that the elements line up from the top left (like a WrapPanel) rather than centering, but that's a minor detail.
(And now try adding a Border, both inside and outside of the Viewbox, and see what I mean there.)
Actually a WrapPanel almost does what I want, except that it auto-sizes the items too large when the window gets smaller.

How do I make a TextBox that can grow wider than its parent?

Is there any way I can make a WPF TextBox dynamically grow beyond the bounds of its parent?
For example, I am allowing a user to type in an XPath string that could be very long (wide), I would like one of two possible things to happen:
The textbox could grow wider when the user types a certain amount of characters.
or
When the user initially clicks on the textbox, it kind of 'pops-out' and is very wide, wider than its container.
Is this possible?
You can set a negative Margin to allow it to grow wider than its parent.
<Grid>
<Grid Margin="50">
<TextBlock Margin="0,0,-50,0" Text="This is a very long text." />
</Grid>
</Grid>
You can try the following:
Put both the ParentControl and the TextBox inside a Canvas.
Locate the TextBox using fixed or dynamic coordinates relative to the ParentControl.
Set the TextBox Z-Index to higher value of the ParentControl.
Relocate and/or expend the TextBox based on TextBox events.

Resources