Created
May 26, 2016 17:19
-
-
Save cwensley/bce3bba2c243c6d8909ddd8c42ec54b6 to your computer and use it in GitHub Desktop.
Eto.Forms example of a DropDown that updates one of its item text using logic in the view model.
This file contains hidden or 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; | |
using Eto.Forms; | |
using Eto.Drawing; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Windows.Input; | |
using System.Linq; | |
using System.ComponentModel; | |
using System.Runtime.CompilerServices; | |
namespace DropDownWithItemUpdate | |
{ | |
public enum Resolution | |
{ | |
Viewport, | |
Custom, | |
R640X480, | |
R800X600, | |
R1024X768, | |
R1280X960, | |
R1600X1200, | |
R2048X1536, | |
R2560X1920, | |
R3200X2400, | |
R852X480, | |
R1280X720, | |
R1366X768, | |
R1920X1080 | |
} | |
public class MyModel : INotifyPropertyChanged | |
{ | |
public MyModel() | |
{ | |
ResolutionNames = new ObservableCollection<ListItem> | |
{ | |
new ListItem { Key = Resolution.Viewport.ToString(), Text = "Viewport" }, | |
new ListItem { Key = Resolution.Custom.ToString(), Text = "Custom" }, | |
new ListItem { Key = Resolution.R640X480.ToString(), Text = "640 x 480"}, | |
new ListItem { Key = Resolution.R800X600.ToString(), Text = "800 x 600"}, | |
new ListItem { Key = Resolution.R1024X768.ToString(), Text = "1024 x 768"}, | |
new ListItem { Key = Resolution.R1280X960.ToString(), Text = "1280 x 960"}, | |
new ListItem { Key = Resolution.R1600X1200.ToString(), Text = "1600 x 1200"}, | |
new ListItem { Key = Resolution.R2048X1536.ToString(), Text = "2048 x 1536"}, | |
new ListItem { Key = Resolution.R2560X1920.ToString(), Text = "2560 x 1920"}, | |
new ListItem { Key = Resolution.R3200X2400.ToString(), Text = "3200 x 2400"}, | |
new ListItem { Key = Resolution.R852X480.ToString(), Text = "852 x 480"}, | |
new ListItem { Key = Resolution.R1280X720.ToString(), Text = "1280 x 720"}, | |
new ListItem { Key = Resolution.R1366X768.ToString(), Text = "1366 x 768"}, | |
new ListItem { Key = Resolution.R1920X1080.ToString(), Text = "1920 x 1080"} | |
}; | |
} | |
Resolution resolution; | |
public Resolution Resolution | |
{ | |
get { return resolution; } | |
set { | |
if (resolution != value) { | |
resolution = value; | |
OnPropertyChanged (); | |
} | |
} | |
} | |
public ObservableCollection<ListItem> ResolutionNames { get; } | |
public ICommand UpdateResolutionCommand | |
{ | |
get { | |
int count = 0; | |
return new Command((sender, e) => { | |
ResolutionNames[0] = new ListItem { | |
Key = Resolution.Viewport.ToString(), | |
Text ="Viewport " + count++ | |
}; | |
OnPropertyChanged(nameof(Resolution)); | |
}); | |
} | |
} | |
void OnPropertyChanged([CallerMemberName] string name = null) | |
{ | |
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name)); | |
} | |
public event PropertyChangedEventHandler PropertyChanged; | |
} | |
public class MainForm : Form | |
{ | |
public MainForm () | |
{ | |
Title = "My Eto Form"; | |
ClientSize = new Size (400, 350); | |
Menu = new MenuBar (); | |
var updateItemButton = new Button { Text = "Update" }; | |
updateItemButton.BindDataContext (c => c.Command, (MyModel m) => m.UpdateResolutionCommand); | |
var combo = new DropDown (); | |
combo.BindDataContext (c => c.DataStore, (MyModel m) => m.ResolutionNames); | |
combo.SelectedKeyBinding.BindDataContext (Binding.Property((MyModel m) => m.Resolution).EnumToString(), defaultContextValue: string.Empty); | |
Content = new StackLayout { | |
HorizontalContentAlignment = HorizontalAlignment.Stretch, | |
Items = { | |
"Combo", | |
combo, | |
updateItemButton | |
} | |
}; | |
DataContext = new MyModel (); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment