WPF Binding to multidimensional array in the xaml -
i have trouble formulate xaml string link specific element in multidimensional array.
the datacontext contains following lines:
private string[] _onedimension = { "[0]", "[1]" }; private string[][] _jagged = { new string[] { "[0,0]", "[0,1]" }, new string[] { "[1,0]", "[1,1]" } }; private string[,] _twodimension = { { "[0,0]", "[0,1]" }, { "[1,0]", "[1,1]" } }; public string[] onedimension { { return _onedimension; } } public string[][] jagged { { return _jagged; } } public string[,] twodimension { { return _twodimension; } }
the xaml contains following lines:
<stackpanel> <button content="{binding onedimension[1]}" width="100" height="50" /> <button content="{binding jagged[1][1]}" width="100" height="50" /> <button content="{binding twodimension[1][1]}" width="100" height="50" /> </stackpanel>
the binding onedimension
, jagged
work expected. binding twodimension
not work , appears wrong, xaml not allow me use separator ,
not know how bind 2 dimensional array.
this:
<button content="{binding twodimension[1,1]}" width="100" height="50" />
does not compile because xaml gets interpreted having 2 arguments binding constructor. there way escape parser or there way of writing not aware of?
edit:
i found out possible escape separator this
<button content="{binding twodimension[1\,1]}" width="100" height="50" />
or surround argument markers this
<button content="{binding 'twodimension[1,1]'}" width="100" height="50" />
however line leads exception: system.argumentexception
{"das array war kein eindimensionales array."} unfortunatelly c# installed in native language - annoying shit... translates {"the array not onedimensionale array."}
is impossible bind multidimensional arrays?
you can bind 2 dimensional array using multivalueconverter defined :
class multidimensionalcoverter:imultivalueconverter { public object convert(object[] values, type targettype, object parameter, cultureinfo culture) { return (values[0] string[,])[(int) values[1], (int) values[2]]; } public object[] convertback(object value, type[] targettypes, object parameter, cultureinfo culture) { throw new notimplementedexception(); } }
that multidimensionalcoverter 3 parameters, 2 dimention array plus 2 indexes, , xaml :
<window.resources> <wpfapp:multidimensionalcoverter x:key="multidimensionalcoverter"/> </window.resources> <grid> <stackpanel> <button content="{binding onedimension[1]}" width="100" height="50" /> <button content="{binding jagged[1][1]}" width="100" height="50" /> <button width="100" height="50" > <button.resources> <system:int32 x:key="1">1</system:int32> </button.resources> <button.content> <multibinding converter="{staticresource multidimensionalcoverter}"> <binding path="twodimension"/> <binding source="{staticresource 1}"/> <binding source="{staticresource 1}"/> </multibinding> </button.content> </button> </stackpanel> </grid>
defining indexes properties in vm more appropriate, using fixed value demonstrate.
Comments
Post a Comment