c# - Is initializing a StackPanel in a converter and present it using a ContentPresenter is a good MVVM practice? -



c# - Is initializing a StackPanel in a converter and present it using a ContentPresenter is a good MVVM practice? -

so kind of follow-up question question lastly time here how vertically “center” align multi line text

at lastly time there's comment answerer :

exactly that, plus should not manipulate ui elements in code, instead create familiar mvvm

the answerer have answered nicely using ellipsegeometry , path. but, since not familiar path, trying find way more "fit" style.

and found way :

converter class

public class noteoctaveconverter : ivalueconverter { private const double _dotdiameter = 3; private readonly thickness _dotmargin = new thickness(0.25); private solidcolorbrush _dotfill; private readonly horizontalalignment _dothzalign = horizontalalignment.center; public object convert(object value, type targettype, object parameter, cultureinfo culture) { int octave = (int)value; list<ellipse> dots = new list<ellipse>(); stackpanel stack = new stackpanel(); if ((octave > 0 && parameter string == "top") || (octave < 0 && parameter string == "bot")) { _dotfill = brushes.black; } else { _dotfill = brushes.transparent; } (int = 0; < math.abs(octave); i++) { dots.add(new ellipse()); dots[i].width = _dotdiameter; dots[i].height = _dotdiameter; dots[i].margin = _dotmargin; dots[i].fill = _dotfill; dots[i].horizontalalignment = _dothzalign; stack.children.add(dots[i]); } homecoming stack; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new notsupportedexception(); } }

and how nowadays view :

<contentpresenter grid.column="1" grid.row="2" content="{binding path=musicalnotation.octave, converter={staticresource noteoctaveconverter}, converterparameter=bot, mode=oneway}" horizontalalignment="center" verticalalignment="top"/>

idk if answerer means "not manipulate ui elements in code" (i'm new mvvm), but, way* acceptable from mvvm point of view since want seek utilize mvvm in way?

*)this way = nowadays stackpanel converter using contentpresenter.

additional notes :

the reason utilize contentpresenter way instead of path need understand work well, since may have nowadays lecturer in presentation session (i don't want nowadays new (risky), if have possible method more comfortable me).

if turns out using stackpanel in way bad practice, please tell me reason is.

thank you.

i'd recommend utilize datatemplate on content presenter content

<contentpresenter content="{binding x}"/> <contentpresenter.contenttemplate> <datatemplate > <stackpanel>...</stackpanel> </datatemplate> </contentpresenter.contenttemplate> </contentpresenter/>

you need dynamic collection handling within stackpanel - accomplish read bind collection stackpanel. such question answered.

about "is practice homecoming controls dataconverter":

i doubt. because converters not designed homecoming full-blown controls - task of datatemplates. well, can used more simple task - additional formatting, or value color. when create controls create violation of mvvm , "maximum of ui in xaml" principles. , in case no advantage. worse - in place of clear xaml , viewmodel logic behind have hard read conversion method.

c# wpf mvvm ivalueconverter

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

c# - Create a Notification Object (Email or Page) At Run Time -- Dependency Injection or Factory -

Set Up Of Common Name Of SSL Certificate To Protect Plesk Panel -