c# - SizeToContent paints an unwanted border -
whenever try make window , set sizetocontent widthandheight, on opening window correctly sizes it's contents, adds small border right , bottom. on resizing disappears, , when using set height , width problem doesn't occur.
this sample of mean:

you not huge problem, though find makes application unprofessional, when need present this. know why happening, or whether there workaround? coding project in c#.
xaml code:
<window x:class="fpricing.inputdialog" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="inputdialog" width="400" height="300" sizetocontent="widthandheight"> <stackpanel> <label x:name="question">?</label> <textbox x:name="response"></textbox> <button content="ok" isdefault="true" click="button_click" /> </stackpanel> </window> values passed on on creation of class.
however experience problem on every window have ever created, without custom underlying code.
using this tool (it's good, btw) found border control of window (it's immediate child) doesn't fill whole window, leaving "border", background of window control.
i've found workaround. width , height of border nan. if set integer value, "border" disappears.
let's use values of actualwidth , actualheight, rounded integer.
define converter:
c#
[valueconversion(typeof(double), typeof(double))] public class roundconverter : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { return math.ceiling((double)value); } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { return value; } } xaml (remember include namespace, in case "c")
<c:roundconverter x:key="roundconverter"/> then create style binding size actual size using converter. it's important use key, won't apply every border (most controls use it):
<style targettype="{x:type border}" x:key="windowborder"> <setter property="width" value="{binding relativesource={relativesource self}, path=actualwidth, converter={staticresource roundconverter}}"/> <setter property="height" value="{binding relativesource={relativesource self}, path=actualheight, converter={staticresource roundconverter}}"/> </style> finally, apply style first child of window (the border control):
private void window_loaded(object sender, routedeventargs e) { getvisualchild(0).setvalue(styleproperty, application.current.resources["windowborder"]); } if can in simpler way, please share too.
Comments
Post a Comment