Archive for the WPF Category

TuneMachine

Posted in .NET, C#, Melody Mine, Music, Programming, Windows, WPF on 04/07/2020 by imekon8

I’ve started writing a new tool to help generate MIDI output. The song part consists of chunks of patterns that get output to MIDI when play is activated.

song

Patterns describe the notes to output:

pattern

These are notes – but not as you’d know them. The scale and mode (see the top) describe which notes get used (in this case C Major). That way any notes you play are always “musical” as the scale isn’t chromatic (i.e. all the twelve notes) but on a scale.

Adventures in WPF

Posted in .NET, C#, Programming, WPF on 25/04/2010 by imekon8

First steps in hooking up code to UI in WPF. Here’s the XAML for the main window:

<Window x:Class="test3.MainWindow"
 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 xmlns:custom="clr-namespace:test3"
 Title="MainWindow" Height="190" Width="177">
 <Window.Resources>
 <CollectionViewSource
    Source="{Binding Source={x:Static Application.Current}, Path=MainWindow.Platforms}"
    x:Key="platforms"/>
 <DataTemplate x:Key="platformTemplate" DataType="{x:Type custom:Platform}">
    <StackPanel Orientation="Horizontal">
       <CheckBox Margin="1" IsChecked="{Binding Path=Selected}"/>
       <TextBlock Margin="1" Text="{Binding Path=Name}"/>
    </StackPanel>
 </DataTemplate>
 </Window.Resources>
    <Grid>
       <Grid.RowDefinitions>
          <RowDefinition />
          <RowDefinition Height="23" />
          <RowDefinition Height="23" />
       </Grid.RowDefinitions>
    <ListBox Grid.Row="0"
       ItemsSource="{Binding Source={StaticResource platforms}}"
       ItemTemplate="{StaticResource platformTemplate}"/>
    <Button Click="OnBuild" Grid.Row="1">Build...</Button>
    <Button Click="OnTogglePC" Grid.Row="2">Toggle PC</Button>
 </Grid>
</Window>

Here’s the code behind the above:

public partial class MainWindow : Window
 {
    ObservableCollection<Platform> m_platforms;

    public MainWindow()
    {
       m_platforms = new ObservableCollection<Platform>();

       m_platforms.Add(new Platform("PC"));
       m_platforms.Add(new Platform("PS3"));
       m_platforms.Add(new Platform("Xbox 360"));

       InitializeComponent();
    } 

    public ObservableCollection<Platform> Platforms
    {
       get { return m_platforms; }
       set { m_platforms = value; }
    }

    private void OnBuild(object sender, RoutedEventArgs e)
    {
       string text = "";

       foreach (Platform platform in m_platforms)
       {
          if (platform.Selected)
          {
             text += platform.Name + " ";
          }
       }

       if (text == "")
       {
          text = "none";
       }

       MessageBox.Show(text, "WPF TEST");
    }

    private void OnTogglePC(object sender, RoutedEventArgs e)
    {
       m_platforms[0].Selected = !m_platforms[0].Selected;
    }
 }

Here’s the Platform class:

public class Platform : INotifyPropertyChanged
{
    private string m_name;
    private bool m_selected;

    public Platform(string name)
    {
       m_name = name;
       m_selected = false;
    }

    public string Name
    {
       get { return m_name; }
       set
       {
          m_name = value;

          OnPropertyChanged("Name");
       }
    }

    public bool Selected
    {
       get { return m_selected; }
       set
       {
          m_selected = value;

          OnPropertyChanged("Selected");
       }
    }

    private void OnPropertyChanged(string name)
    {
       if (PropertyChanged != null)
       {
          PropertyChanged(this, new PropertyChangedEventArgs(name));
       }
    }

    public event PropertyChangedEventHandler PropertyChanged;
 }

…and here’s what it looks like:

If you change the check boxes by each platform, then click on “Build”, you get a message box indicating which boxes you ticked; if you click on the “Toggle PC”, you’ll see the check box by PC changing.