c# - UserControl renders with no width in designer -
i have uwp usercontrol
contains path element, data
property of path bound string property of usercontrol
called icon
. when add control page , set it's icon
property resource item, control doesn't render icon , has 0 width in designer. when deploy application device control renders expected. there way fix this?
for reference, i'm trying build simple toolbar has bunch of clickable icons. i'm sure there other ways of achieving i'm using learning xaml skills pretty lacking. code can found below.
mainpage.xaml
<stackpanel grid.column="1" orientation="horizontal" horizontalalignment="center" verticalalignment="stretch"> <local:actionicon icondata="{staticresource test}" ></local:actionicon> </stackpanel>
actionicon.xaml
<usercontrol x:name="usercontrol" x:class="uwptest.actionicon" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:uwptest" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" d:designheight="200" d:designwidth="200"> <viewbox stretch="uniformtofill"> <path stretch="uniformtofill" data="{binding icondata, elementname=usercontrol}" fill="black" /> </viewbox> </usercontrol>
actionicon.xaml.cs
public sealed partial class actionicon : usercontrol { public actionicon() { initializecomponent(); } public string icondata { { return (string) getvalue(icondataproperty); } set { setvalue(icondataproperty, value); } } public static readonly dependencyproperty icondataproperty = dependencyproperty.register( "icondata", typeof(string), typeof(actionicon), new propertymetadata(default(string))); }
resourcedictionary entry
<x:string x:key="test">m10,16.5v7.5l16,12m12,2a10,10 0 0,0 2,12a10,10 0 0,0 12,22a10,10 0 0,0 22,12a10,10 0 0,0 12,2z</x:string>
in nutshell, problem types of icondata , path.data don't match. while icondata string, path.data wants geometry. if want put path's data in resource dictionary, type has geometry or 1 of subclasses. consider, wpf won't throw exception when binding fails. instead message in output-window of visual studio.
but why can use string when set path.data-property directly?
the path never gets string. when xaml parser gets wrong type property, looks @ class of type expected. it's searching there typeconversion-attribute. when parser looks @ geometry, finds such attribute:
[typeconverter(typeof(geometryconverter))] abstract partial class geometry
the attribute specifies typeconverter. geometry geomtryconverter, can convert strings geometry. there's article @ msdn that, if want know more: how to: implement type converter
fine, how use geomtries in resourcedictionaries?
the thing becomes clear once try create path without asiging string path.data. simple bezier curve before:
<path data="m 10,100 c 10,300 300,-200 300,100" />
and after:
<path> <path.data> <pathgeometry> <pathfigure startpoint="10,100"> <beziersegment point1="10,300" point2="300,-200" point3="300,100"/> </pathfigure> </pathgeometry> </path.data> </path>
the typeconverter produces such output. points again converted typeconverter. however, next step simple: put pathgeometry in resourcedictionary.
as little side node: can avoid using elementname every binding setting user controls datacontext itself:
<usercontrol datacontext={binding relativesource={relativesource self}}" />
Comments
Post a Comment