blob: d24177f8227944dfddbb17a8d83f7347203f112b (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
using System;
using System.Collections.Generic;
using System.Linq;
using Cadenza.Collections;
using Xamarin.PropertyEditing.Resources;
namespace Xamarin.PropertyEditing.ViewModels
{
internal class PredefinedValuesViewModel<TValue>
: PropertyViewModel<TValue>
{
public PredefinedValuesViewModel (IPropertyInfo property, IEnumerable<IObjectEditor> editors)
: base (property, editors)
{
this.predefinedValues = property as IHavePredefinedValues<TValue>;
if (this.predefinedValues == null)
throw new ArgumentException (nameof(property) + " did not have predefined values", nameof(property));
UpdateValueName();
}
public IEnumerable<string> PossibleValues
{
get { return this.predefinedValues.PredefinedValues.Keys; }
}
public string ValueName
{
get { return this.valueName; }
set
{
if (value == this.valueName)
return;
TValue realValue;
if (!this.predefinedValues.PredefinedValues.TryGetValue (value, out realValue)) {
if (this.predefinedValues.IsConstrainedToPredefined) {
SetError (Strings.InvalidValue (value));
return;
}
// TODO: Figure out where the conversion needs to happen
} else
Value = realValue;
this.valueName = value;
OnPropertyChanged ();
}
}
public bool IsConstrainedToPredefined => this.predefinedValues.IsConstrainedToPredefined;
// TODO: Combination (flags) values
protected override TValue ValidateValue (TValue validationValue)
{
if (!this.predefinedValues.IsConstrainedToPredefined || IsValueDefined (validationValue))
return validationValue;
return Value;
}
protected override void OnValueChanged ()
{
base.OnValueChanged ();
if (this.predefinedValues == null)
return;
UpdateValueName();
}
private string valueName;
private readonly IHavePredefinedValues<TValue> predefinedValues;
private bool IsValueDefined (TValue value)
{
return this.predefinedValues.PredefinedValues.Values.Contains (value);
}
private bool TryGetValueName (TValue value, out string name)
{
name = null;
foreach (var kvp in this.predefinedValues.PredefinedValues) {
if (Equals (kvp.Value, value)) {
name = kvp.Key;
return true;
}
}
return false;
}
private void UpdateValueName ()
{
string newValueName;
if (TryGetValueName (Value, out newValueName)) {
this.valueName = newValueName;
OnPropertyChanged (nameof(ValueName));
}
}
}
}
|