Last active
November 12, 2021 11:44
-
-
Save CheetahChrome/1a742384c2ead952972c9d98acd6fc67 to your computer and use it in GitHub Desktop.
WPF Menu and Submenu Styling as Blue Gradient Background
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MRU | |
{ | |
public string Name { get; set; } | |
public string Address { get; set; } | |
public string Data { get; set; } | |
public bool IsValid => !string.IsNullOrWhiteSpace(Name); | |
public bool IsFile => !string.IsNullOrWhiteSpace(Address); | |
public bool IsData => !IsFile; | |
public MRU(string address) | |
{ | |
if (string.IsNullOrWhiteSpace(address) | |
|| !File.Exists(address)) return; | |
Address = address; | |
Name = System.IO.Path.GetFileName(address); | |
} | |
public override string ToString() => Name; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Install-Package Ben.Demystifier -Version 0.4.1 | |
// The VM's Error is a notified string. This example | |
// can use the visual error box (placed hidden at the bottom of the main page until an error is in the error property) | |
// - Error box example at https://gist.github.com/CheetahChrome/e21884ab06e85e27cb2c950ea0b01c60 | |
private void SelectMRU(object sender, RoutedEventArgs e) | |
{ | |
try | |
{ | |
var mru = (e.OriginalSource as MenuItem).DataContext as MRU; | |
if (mru is not null) | |
{ | |
VM.ClearAll(); | |
this.Title = mru.Name; | |
ShowJSON(File.ReadAllText(mru.Address)); | |
} | |
} | |
catch (Exception ex) | |
{ | |
VM.Error = ex.Demystify().ToString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.ComponentModel; | |
using System.Collections.ObjectModel; | |
using System.Runtime.CompilerServices; | |
using System.Text.Json; | |
using System.Windows.Input; | |
using Microsoft.Expression.Interactivity.Core; | |
public class MainVM : INotifiedPropertyChanged | |
{ | |
private ObservableCollection<MRU> _MRUS; | |
public ObservableCollection<MRU> MRUS | |
{ | |
get { return _MRUS; } | |
set { _MRUS = value; OnPropertyChanged(nameof(MRUS)); } | |
} | |
// One can optionally use the visual error box (placed hidden at the bottom of the main page until an error is in the error property) | |
// - Error box example at https://gist.github.com/CheetahChrome/e21884ab06e85e27cb2c950ea0b01c60 | |
private string _Error; | |
public string Error | |
{ | |
get => _Error; | |
set { _Error = value; OnPropertyChanged(nameof(Error)); OnPropertyChanged(nameof(HasError)); } | |
} | |
public bool HasError => !string.IsNullOrEmpty(Error); | |
public VM() | |
{ | |
MRUS = new ObservableCollection<MRU>(GetMRUS()); | |
} | |
public List<MRU> GetMRUS() | |
{ | |
try | |
{ | |
var mruText = Properties.Settings.Default.MRUS; | |
return string.IsNullOrWhiteSpace(mruText) ? new List<MRU>() | |
: JsonSerializer.Deserialize<List<MRU>>(mruText); | |
} | |
catch (Exception ex) | |
{ | |
Error = ex.ToStringDemystified(); | |
} | |
return null; | |
} | |
// Add a user `Setting` (in the project properties) of `MRUS` of type `string` as `User` setting. | |
public void AddMRUToUserSettings(MRU mRU) | |
{ | |
try | |
{ | |
MRUS.Add(mRU); | |
var asList = MRUS.ToList<MRU>(); | |
Properties.Settings.Default.MRUS = JsonSerializer.Serialize(asList); | |
Properties.Settings.Default.Save(); | |
} | |
catch (Exception ex) | |
{ | |
Error = ex.Demystify().ToString(); | |
} | |
} | |
/// <summary> | |
/// Event raised when a property changes. | |
/// </summary> | |
public event PropertyChangedEventHandler PropertyChanged; | |
/// <summary> | |
/// Raises the PropertyChanged event. | |
/// </summary> | |
/// <param name="propertyName">The name of the property that has changed.</param> | |
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "") | |
=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This WPF based Gist creates a menu bar with an MRU sub list which is saved to the User Settings by Json serialization which looks like this: