2

INTRODUCTION


So, guys, I am making an application to show musical notes, for example :

enter image description here

PROBLEM EXPLANATION


Okay, for "drawing that image", it have to broken down into some parts (because it's impossible to given "preformatted image, since musical notation is really various), like below.

enter image description here

THE CLASSES


Let's say "Red" is a "NoteHead", "Blue" is a "NoteBeam" and "Yellow" is a "NotePole". Given those, these are my mockup classes :

public class MainWindowVM : ViewModelBase
{
    public ObservableCollection<Mockup> MockupCollection { get; set; } 
}

public abstract class Mockup 
{ 
    public Point TopLeftCanvasCoordinate { get; set; }
}

public class NoteHead : Mockup 
{
    //A note can be black or transparent filled
    public SolidColorBrush NoteFill { get; set; } 
}

public class NoteBeam : Mockup 
{ 
    public Point LineStartCoordinate { get; set; }
    public Point LineEndCoordinate { get; set; }
    public double LineThickness { get; set; }
}

public class NotePole : Mockup 
{
    public Point LineStartCoordinate { get; set; }
    public Point LineEndCoordinate { get; set; }
    public double LineThickness { get; set; }
}

Let's assume the Point class above have both X and Y are double, not int.

THE XAML


  1. NoteHead always Binded to an Ellipse

    <Ellipse Width="10" Height="10" Fill="{Binding NoteFill}" Stroke="Black" StrokeThickness="1"> <Ellipse.RenderTransform> <SkewTransform AngleX="-15" CenterX="5" CenterY="5"/> </Ellipse.RenderTransform> </Ellipse>

  2. NotePole and NoteBeam always Binded to a Line

    <Line X1="{Binding LineStartCoordinate.X}" Y1="{Binding LineStartCoordinate.Y}" X2="{Binding LineEndCoordinate.X}" Y2="{Binding LineEndCoordinate.Y}" Stroke="Black" StrokeThickness="{Binding LineThickness}"/>

Assume all Canvas.Top and Canvas.Left above put in the ItemsControl section

So, the reason why I want to use ItemsControl is because I don't know how many Mockups will be drawn in the canvas. It's totally varies.

1 ItemsControl can contain more than 1 Musical Notation. (It doesn't have limitation afterall). As long as the collection it binded to have 100 items (mixed along between Head, Pole, and Beam), it will draw them all to the Canvas. (Btw, if you understand music, then 1 canvas contains of 1 measure / bar, otherwise it doesn't really matter to the solution)

THE QUESTIONS


  1. How to properly build the ItemsControl to represent such structure? *The problem, I think lies on the shape variety, either Ellipse or Line. Because, as far as I know, a DataTemplate can only contains 1 UIElement.
  2. How to properly "attach" Canvas properties to the control in ItemsControl? I read this similar SO post, but when I tried it out, it doesn't work. Please include the setting for Canvas properties in your solution.

ADDITIONAL NOTE


Maybe the solution I aimed will look something like this. It's possibly not the correct one I know. It's just to give you a more concrete view on what the solutin should look like.

    <ItemsControl Grid.Column="0" Grid.Row="0">
        <ItemsControl.ItemsPanel>
            <ItemsPanelTemplate>
                <Canvas />
            </ItemsPanelTemplate>
        </ItemsControl.ItemsPanel>
        <ItemsControl.ItemContainerStyle>
            <Style TargetType="ContentPresenter">
                <Setter Property="Canvas.Left" 
                        Value="{Binding Path=TopLeftCanvasCoordinate.X}" />
                <Setter Property="Canvas.Top" 
                        Value="{Binding Path=TopLeftCanvasCoordinate.Y}" />
            </Style>
        </ItemsControl.ItemContainerStyle>
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <!--THIS SHOULD BE FILLED WITH MOCKUP (HEAD / POLE / BEAM)-->
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

Thanks for reading this long post.

Community
  • 1
  • 1
Moses Aprico
  • 1,951
  • 5
  • 30
  • 53
  • I see that you have put a lot of detail into your question but it's still really unclear. Does one musical note = one itemscontrol? "a user control can only contains 1 data type" -- A user control doesn't contain types. "How to properly "attach" Canvas properties to the control in ItemsControl" -- what canvas? a control in an items control? some example code would really help here – Julien Nov 05 '14 at 17:20
  • @Julien thank you for your response. I've edited my post. I hope it make you clearer on what I'm going through here. :D – Moses Aprico Nov 05 '14 at 17:59
  • so to clarify further, a `Mockup` is a note? And a note is comprised of collections of Beams, Heads, and Poles. Shouldn't you have a view model containing these 3 collections to represent one note? – Julien Nov 05 '14 at 19:01
  • @Julien a mockup is a class to represent the fragments of note. It's just a media for xaml to bind to since in mvvm, I can't add an `UIElement` directly. I have a viewmodel, but not on each 1 note, because the drawing of the note is easier if I draw them in the parent VM. (The mainwindow, which is a VM that can read a set of notation). Oh, and a note doesn't always have beam or pole. but it has at least 1 head. – Moses Aprico Nov 06 '14 at 01:08
  • You can use a data template selector to select a template for your ItemsControl.ItemTemplate based on the VM type that it is referencing. So for example you could have a xaml template that is an Ellipse, and another xaml template that is a Line, and your Mockup VM will need all the properties so that the template can draw itself in the correct position – Julien Nov 06 '14 at 15:57
  • @Julien Hi there! I've solved this problem! I'll post my answer soon.. Thanks for giving me insights!! :) – Moses Aprico Nov 06 '14 at 16:07
  • 1
    @MosesAprico how soon tho? – AgentFire Apr 16 '17 at 19:28
  • @AgentFire LOL I forgotten. And now I already left this project untouched for a very long time so it's doubtful that I still remember it. Better re-ask it if you have the same / similar problem. – Moses Aprico Apr 17 '17 at 16:08
  • @MosesAprico nvm, I found my solution. – AgentFire Apr 18 '17 at 16:43

0 Answers0