I attached my XAML and .CS sample code so you can see what is it all about. I have MainWindow with at least 10 buttons (one part for media element, second for playlist, and third for application itself. Also, there are helper methods I use in all these mentioned methods.
It gets really ugly having only one .cs file with dozens of methods. I have no idea how to separate these and still keep communication between controls and methods.
I have Main Window like this in XAML
<Window x:Class="Modulated.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="194" Width="525">
<Grid>
<Button Content="Sort Asc" Height="23" HorizontalAlignment="Left" Margin="10,12,0,0" Name="button3" VerticalAlignment="Top" Width="75" Click="sort_asc" />
<Button Content="Sort Desc" Height="23" HorizontalAlignment="Left" Margin="12,42,0,0" Name="button4" VerticalAlignment="Top" Width="75" />
<ListBox Height="131" HorizontalAlignment="Left" Margin="99,12,0,0" Name="listBox1" VerticalAlignment="Top" Width="70" >
<ListBoxItem>1</ListBoxItem>
<ListBoxItem>2</ListBoxItem>
<ListBoxItem>3</ListBoxItem>
</ListBox>
<MediaElement Height="131" HorizontalAlignment="Left" Margin="187,12,0,0" Name="mediaElement1" VerticalAlignment="Top" Width="165" />
<Button Content="Play" Height="23" HorizontalAlignment="Left" Margin="372,12,0,0" Name="btnPlay" VerticalAlignment="Top" Width="75" Click="play" />
<Button Content="Stop" Height="23" HorizontalAlignment="Left" Margin="372,42,0,0" Name="btnStop" VerticalAlignment="Top" Width="75" Click="stop" />
<Button Content="Next" Height="23" HorizontalAlignment="Left" Margin="372,71,0,0" Name="btnNext" VerticalAlignment="Top" Width="75" Click="next" />
</Grid>
and MainWindow.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Modulated
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void play(object sender, RoutedEventArgs e)
{
mediaElement1.Play();
}
private void stop(object sender, RoutedEventArgs e)
{
mediaElement1.Stop();
}
private void next(object sender, RoutedEventArgs e)
{}
private void sort_asc(object sender, RoutedEventArgs e)
{}
}
}
A good way to separate logic and presentation is the MVVM design pattern. You can find a tutorial on it here:
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx