so this is some kind of follow-up question from my question last time here How to Vertically “center” align the multi line text
At the last time there's this comment from the answerer :
Exactly that, plus that you should not manipulate UI elements in code, but instead make yourself familiar with MVVM
The answerer also have answered nicely using EllipseGeometry
and Path
. But, since I am not that familiar with Path
, I am trying to find another way that more "fit" to my style.
And I found this another way to do it :
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 as string == "TOP") || (octave < 0 && parameter as string == "BOT"))
{
_dotFill = Brushes.Black;
}
else
{
_dotFill = Brushes.Transparent;
}
for (int i = 0; i < 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]);
}
return stack;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotSupportedException();
}
}
And this is how I present it to the 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 this what answerer means by "not manipulate UI Elements in code" (I'm new to MVVM), but, is this way* acceptable from the MVVM point of view since I want to try to use MVVM in a good way?
*)This way = Present a StackPanel
from a Converter
using ContentPresenter
.
Additional Notes :
The reason I use the ContentPresenter
way instead of Path
is that I need to understand my work well, since I may have to present it to my lecturer in a presentation session (I don't want to present something new (risky), IF I have another possible method which is more comfortable to me).
If it turns out that using StackPanel
in my way is a bad practice, please tell me what the reason is.
Thank you.