Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<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.
Posted
Updated 4-Sep-15 22:11pm
v3
Comments
Patrice T 4-Sep-15 17:31pm    
If I understand well, the xml code is not where you have a problem.
What about improve question with the code that gives you a problem ?

1 solution

Problem Solved. I should have realised that although a binding type converter may only be created once, any static fields in the converter can be accessed and changed easily from the view model. So I can now bind a list of time signatures from a combo in the view and use it to propagate my TPB value into the converter. It works like a charm. Sorry to have wasted any ones time.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900