How are width values handed down in wpf - c#

Currently I am struggling to correctly size wpf controls. Therefore I'd like to know how dimensions like width are handed down the hierarchy in an xaml file. What happens for example if a width value is overwritten by a set MinWidth value? How and when are the other elements resized and what events are involved? Thanks for your support.

There is many topics in this question, it's looks like you're looking all information or manual about sizing controls.
For the first question basically a width property for a control is only for that control, not for the child controls. The layout (for example a Grid) of the controls plays an important role because sometimes they stretch, center or moves the controls, but a fixed number width works most of the time on the control you want to resize.
About MinWidth is not overwritten by Width property, yo can set less width of MinWidth, the value is stored but the control will be drawed with the MinWidth value because is higher in that case.
About Events, there is an Event called "SizeChanged" that triggers when the Height or Width is changed, but it's in execution time, there is no event that are triggering in design mode.
Try to practice with Grids, research about layouts like Grids, Canvas, StackPanels, etc. Maybe there is your answer. Canvas let you draw free, but I don't recomend get used to use canvas.

Related

Width of control based on width of window

I'm trying to figure out, how to write Responsive UI in XAML.
I would like to have 2 controls on my window. MinWidth of each control id 400.
If Width of windows is >800, Width of each control should be 50%, if we change widht of window to <800, each control should have 100% of window.
It's pretty simple to set up something like that with HTML/CSS, and right now I'm trying do it in WPF without progress.
Can you help me with that?
I tried to handle that with WrapPanel, but it does not work. It wraps properly, but does not change width of control.
I have two ideas for doing this:
Using a binding converter, and bind the width/height of each item to the container width/height.
Creating a custom panel, with de desired layout.
Only a few ideas, hope helps.
Define resources based on screen revolution.
In design mode, using dynamic key to avoid errors.
In runtime, dynamically add resources into App.

Windows store app, horizontal scrolling when too much text

I am making an app for the Windows store (xaml/c#), in this app i create a list of properties of a file. One properties is the file path, this is always too long. my solution would be to make this side scrolling when this textblock is selected.
the text would start to scroll when selected to make all the information readable. (ticker bar)
I can’t find a property to achieve this, and I find it difficult to make a custom function for this. Can you help me?
I'd reconsider the idea. I had the idea many times and it was always rejected by designers. I think animating text is likely to be more annoying than clipped text. A better way might be to either wrap it in place or if you are in a list with limited item height - only show the full text after it was tapped - either with some popup or in the details view of the tapped item.
Having said that - to animate it you'd put the TextBlock inside of a Canvas, set the Clip of the Canvas to a RectangleGeometry that defines your clipping region (and update it on size updates), then animate the Canvas.X property of the TextBlock. The problem with that is that Canvas.X is not an "independent" property and so you'd need to set DoubleAnimation.EnableDependentAnimation="true" on the animation and could still get a fairly choppy animation.
A better choice might seem to be to animate the RenderTransform of the TextBlock (e.g. set it to a TranslateTransform and target the X property of the transform in the animation), but last time I checked - this could animate the TextBlock in an already clipped form, so you still wouldn't see the part of it that doesn't fit on screen. You could try that though.
Putting the TextBlock in a ScrollViewer might work better, but it would suffer from similar problem as the Canvas solution since the ScrollViewer doesn't even have an animatable offset property. In Windows 8.1 you get built in animations when you call ScrollViewer.ChangeView(), but that would likely animate too quickly. It is possible with the help of a proxy dependency property or other per-frame dependent animation as in the ScrollToHorizontalOffsetWithAnimation() ScrollViewer extension method in WinRT XAML Toolkit.

Window with dynamic size to keep its current size after loaded

I have a Window that has dynamic width and height and a user control as content.
Based on several conditions in my view model several controls of my user control are visible or not visible.
Using SizeToContent.WidthAndHeight nicely WPF make my window appears perfectly with the correct dimensions to fit everything that has to be visible according to my Bindings that take effect during Loaded event of my user control.
My problem is that I have a TextBox with no defined width where if the user types in too many characters it forces my Window to grow in width, and I don't want that!
Is there a way to avoid that without writing to much code and without loosing the functionality that if user resizes the window, everything in there change automatically to fit the new size?
What I want is that every control (or the window itself) will keep its current size and not to grow outside of current window's bounds.
I hope I made clear my problem and thank you in advance!
I think you are searching for the MaxWidth property of the TextBox, try to bind it to the Textbox-es parent controls Width and then if you type a longer text then it shouldn't grow larger.

width of the dropdown arrow

So in winforms, every dropdown combobox has this little arrow thingy to the right that tells the user it's a dropdown, kinda like this:
Now how do I figure out how wide that is in pixels? Reason is, I'm using ControlDrawToBitmap, this doesn't draw the text properly for the combo boxes, and I can redraw the contents, I just whack some of the arrows (which are drawn properly).
First idea that comes to mind: Check to see if the combobox button width tracks with the scrollbar width. The scrollbar width can be modified in user preferences. Use GetSystemMetrics() API to get the width of the various scrollbar pieces. If you change your system scrollbar width and it does not affect the size of a normal combobox, then ignore this.
Second idea: use the edit control's formatting rect to find out what the edit control thinks is the usable display area (minus the combo box). See EM_GETRECT in MSDN.
However, it sounds like this is just a hack workaround for your real problem: If you could get the controls to draw correctly to bitmap, then you wouldn't need this hackery.
I calculated it to be 9 pixels wide in photoshop

Multiple column scrolling, resizeable panel?

I have this:
Each list is its own WrapPanel and they are all on another WrapPanel which is in a ScrollViewer. If I don't set the height myself for the main WrapPanel it assumes I want the WrapPanel as high as it can go giving me only one column whereas I want as many columns as needed to fill the window.
If I set the Width and Height of the WrapPanel that holds everything to fixed numbers, but I want it to change when the user resizes the window.
In your example screen shot and description I see a tab control whose anchor is set to Top, Left, Bottom, and Right. The tab page with AutoScroll set to true. Within the tab page I see a FlowLayoutPanel. The FlowLayoutPanel has its AutoSize property set to true. I also see a set of other panels/user controls each of which contains a title and a series of check boxes.
You can`t achieve this with standard controls. You can try to create your own custom WrapPanel implementation. But, actually, looking at original WrapPanel sources I think this will be quite tricky. You see, what you want, is basically to measure how many columns can fit in the current window, while every element in column can be of any size. The way I see that algorithm, it will require N*N iterations to get the final result. So you may have problems with performance.

Categories

Resources