Skip to content

Instantly share code, notes, and snippets.

@julesx
Created April 10, 2014 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save julesx/10406971 to your computer and use it in GitHub Desktop.
Save julesx/10406971 to your computer and use it in GitHub Desktop.
using System.Collections.ObjectModel;
using System.ComponentModel;
using WpfApplication14.Annotations;
namespace WpfApplication14
{
public class MainVm : INotifyPropertyChanged
{
#region Singleton Setup
private static readonly MainVm Instance = new MainVm();
public static MainVm Current
{
get { return Instance; }
}
#endregion
#region GUI Vars
public ObservableCollection<MenuVm> MenuVms { get; set; }
private MainVm _activeVm;
public MainVm ActiveVm
{
get { return _activeVm; }
set
{
_activeVm = value;
OnPropertyChanged("ActiveVm");
}
}
#endregion
#region C'Tor
public MainVm()
{
MenuVms = new ObservableCollection<MenuVm>();
MenuVms.Add(new MenuVm());
MenuVms.Add(new MenuVm());
MenuVms.Add(new MenuVm());
MenuVms.Add(new MenuVm());
MenuVms.Add(new MenuVm());
}
#endregion
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MainVm.Current.ActiveVm = MainVm.Current; // DOESNT WORK
//DataContext = MainVm.Current; // WORKS
}
}
<Window x:Class="WpfApplication14.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.Resources>
<CollectionViewSource x:Key="MenuVms" Source="{Binding MenuVms}" />
</Grid.Resources>
<Button DataContext="{Binding ActiveVm}" Width="25" Height="25" Name="OptionsButton">
<Button.ContextMenu>
<ContextMenu Placement="Top">
<ContextMenu.ItemsSource>
<CompositeCollection>
<CollectionContainer Collection="{Binding Source={StaticResource MenuVms}}" />
</CompositeCollection>
</ContextMenu.ItemsSource>
</ContextMenu>
</Button.ContextMenu>
<!--<Button.Template>
<ControlTemplate>-->
<Rectangle Width="25" Height="25" Fill="Pink" />
<!--</ControlTemplate>
</Button.Template>-->
</Button>
</Grid>
</Window>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment