<DataTemplate x:Key="sharps">
<StackPanel Orientation="Horizontal" TextBlock.FontSize="30" >
<StackPanel.RenderTransformOrigin>
<Point X="0.5" Y="0.5"/>
</StackPanel.RenderTransformOrigin>
<StackPanel.RenderTransform>
<TranslateTransform Y="{Binding Midi, Converter={StaticResource MidiToOffsetSize}}"/>
</StackPanel.RenderTransform>
<TextBlock FontSize="18" VerticalAlignment="Center" Foreground="Black" Text="{Binding CumTicks,Converter={StaticResource CumTicksConverter}}" />
<TextBlock FontFamily="notess" Text="{Binding Pitch}"/>
<TextBlock Foreground="Red" FontFamily="HarmonyMKT" Text="{Binding Notes}" />
</StackPanel>
</DataTemplate>
This is a data template for displaying a list of musical notes in a sort of word processor fashion. Each note is an instance of the model. Everything is bound to an observable collection of notes in the view model. The first TextBlock in the StackPanel is bound to a Midi field in the model using a Converter (CumulativeTicksConverter) in order to be able to display the bar lines in the music depending on the time signature. The time signature is a string property in the viewmodel which I want to bind to in the view so I can change the time signature easily. My problem is this:- HOW CAN I GET THIS STRING FROM THE VIEW MODEL INTO THE CONVERTER. All I have been able to do is hard code the string as a static property in the viewmodel and reference the instance in the converter. If I try a multibinding, I could pass the string as a binding parameter but how do I pass a non static property in the viewmodel into the data template where all the bindings (I presume) are with properties in the model , not the viewmodel.
The value converter I use in the data template is shown below with the problem field underlined ie TPB ( which stands for midi ticks per bar of music).
public class CumTicksToBarConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int ct = (int)value;
int quotient, remainder;
quotient = Math.DivRem(ct,
MainViewModel.TPB, out remainder);
if (remainder == 0) return quotient + 1; else return "";
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
TPB is a reference to a static field in the viewmodel as shown here.
public static int TPB { get; set; }.
I don't know how to bind to this value in the view in order to change its value. I suppose the converter is only created once in the xaml constructer (am I correct?) but I would like to be able to choose TPB in the view and pass this to the converter BEFORE the converter is created. How do I do this??
Ideally I would like to run a startup screen before the main window loads. The user could type in a title for the song and its time signature but I need help doing this. I would then want to pass this time signature to the property TPB in the viewmodel before the converter is constructed in the main window xaml.
Any help much appreciated.