Any hints for dealing with snap to device pixels in WPF - wpf

Many controls in WPF has this snap to device pixels (as explained on MSDN). I just often find myself forgetting to set it and then later discover that something sometimes looks odd because of this.
What do people use to deal with this issue?

SnapsToDevicePixels property is inherited through the element tree.
I always set SnapsToDevicePixels="True" on each Window, and all the children inherit the value.
(But I don't have any suggestion for how to remember to set it on each Window.)

SnapsToDevicePixels="True" does not trickle down into an OxyPlot grid, however...
Some of the grid lines show up double.
and OxyPlot PlotView's SnapToDevicePixels does not help either.

Related

Set minimum size of control so all childs fit in WPF

Is there a way in wpf to get a panel or other controls to resize only until overlap/clipping of it's childcontrols would happen.
Example: a Horizon StackPanel with 2 controls of size 100 could be no smaller then 200 wide.
I'm looking for an easy way to do this and hope wpf supports this, because it seems a pretty common thing to ask.
If you're gonna provide an answer that tells me to go over all the children and measure them. Please give directions how to take margin into account.
Thanks
EDIT:
without hardcoding it yourself

SnapsToDevicePixels seems to not work in StackPanel

I have more expanders in StackPanel (Orientation=Vertical). When I expand one, the others below the expanded one seem to be blured. Like it would SnapsToDevicePixels=False.
I think the problem could be in StackPanel because it doesn't support UseLayoutRounding in .NET 3.5 (which I use) yet. Is that possible or the problem could be elsewhere?
I put SnapsToDevicePixels="True" almost everywhere I could, but nothing changed.
(Font has size 12 so it couldn't be [1]: WPF Blurry fonts issue- Solutions)
Try set UseLayoutRounding property it should adjust layout to use position rounded to integer.
http://www.switchonthecode.com/tutorials/wpfs-most-important-property-uselayoutrounding
I solved a similar problem with RenderOptions.EdgeMode="Aliased"

Why isn't always possible to set a winforms dimensions?

I'm trying to size a checkboxlist control to 500,250 from its current 502,251 (It's an OCD thing) but every time I try, it just reverts to 502,251.
Is it because the parent container is docked in the window? Are there any workarounds?
(This is through the visual designer)
Most likely the control is being resized due to the control's font size. The ListControl does not like to display an item that will be "split" by the bottom edge, so it will resize the height. Try changing the control's font size and adjust again to verify.
No work around, and you really do not what one, because the control is really doing the right thing.
Yes, it is OCD. I have it also, but this one you have to let go. :O) Consider yourself lucky because you are only one or two pixels off. I was five pixels off once, and I had to put a note on my monitor to ignore it. It so bothered me.

Silverlight Border - Am I stupid?

sorry, I did not find something useful when searching google. Very basic question, mainly "Am I stupid". I know StackPanel gives its child elements full space, but why does the "Auto" property relate to the PARENT element in case of a border. I mean perhaps I am doing something wrong, but this behaviour is definitly not what I intended:
Pic1 http://img6.imageshack.us/img6/171/20090805002723.jpg
Ok, after some thinking I found a way, which looks like this:
Seriously http://img18.imageshack.us/img18/3173/20090805003045.jpg
But really, this way? I mean a "minimal sized control" with a textbox (which width I want to define) and a border around it, and I need this kind of tree? If anyone has a better way, please tell me...
Chris
PS: And that while I am writing about the nice UI composition for Silverlight, and wanted to give a simple example.. I just say: Legendary!
Definitely not stupid. This can be very confusing. I find it helps when thinking about layout in WPF/Silverlight to think top down from the root of the control hierarchy instead of bottom up.
It becomes obvious when you think about the stack panel's job. It stacks up its child elements and sets their widths to its width. It is therefore overriding your border's width of Auto. The Canvas you later wrapped around it does not try to rearrange its children at all, and it does not override their widths, so while its width is the width of the stack panel, the Auto on your border is now working (sized to its content, the TextBox).
Clear as mud?
Here is an article with more detail:
http://msdn.microsoft.com/en-us/library/ms745058.aspx
And I highly recommend the WPF book by Chris Sells & Ian Griffiths to get up to speed on the intricacies of WPF/Silverlight layout.
Canvas sould be avoided unless it really makes sense for what you're trying to do. For example, Canvas normally makes sense for a game, or something where you want to drag elements around. Here it's just getting in your way.
Reasons not to use Canvas:
http://blogs.msdn.com/devdave/archive/2008/05/21/why-i-don-t-like-canvas.aspx
An easy way is to get rid of the Stackpanel and just use the VerticalAlignment and HorizontalAlignment to keep it at the top left. Then just set the Border Width and leave the Height alone.
<Grid x:Name="LayoutRoot">
<Border Width="150" BorderBrush="Blue" BorderThickness="1" VerticalAlignment="Top" HorizontalAlignment="Left">
<TextBox Text="I'm Serious" Background="LightBlue" />
</Border>
</Grid>

WPF - UserControl sizing on Custom Canvas

I have a custom canvas that I can drop controls on, move, and resize. I need to be able to at least start with a pre-determined size but then allow re-sizing.
My problem is that I have a user control that doesn't appear to resize. I set Height and Width on the user control (works on drop). But then as the control is resized on the canvas the visual size stays the same (the sizing handles are changing). I can see the Height property changing (usercontrol.height) as the control is resized. But again, the appearance of the control stays the same size.
I had a thought that the inner container on the user control should be bound to the usercontrol.height but that didn't seem to help. (or maybe I didn't have the binding right).
On the same canvas, if the dropped control is for example, an Image control, I can set an explicit height on the drop and everything resizes as it should. So it seems to be UserControl thing.
So is there something special I need to do with UserControls to get the visuals to resize? Should I be using explicit height and width to get it all started?
Thanks for any help.
The WPF layout system can be confusing, as you've discovered. There is no substitute for a solid understanding of the layout and measurement system. If you plan on doing much WPF development, it's worth taking 20 minutes to read this article on the layout system and then experimenting for a while in an iterative environment such as Kaxaml until you feel comfortable.
The system is very logical. It won't take long for the penny to drop. Sorry for the indirect answer, but I don't believe there's enough information in your question to really explain what's going on.
You might also look at the HorizontalAlignment and VerticalAlignment properties.
Not a WPF expert but I believe you need to enable auto-sizing in order to achieve the scenario you are looking for. This is done by setting the Height/Width of a control to Double.NaN which essentially says, "I have no specific size". Once you do that, the control should resize to occupy available space based on the need of the control.

Resources