[WPF 시리즈] - DataBinding 열거 유형 데이터 소스
7394 단어 bind
public class EnumerationDataProvider : ObjectDataProvider
{
public Type EnumerationType { get; set; }
public string NoneValue { get; set; }
public string NoneDisplayValue { get; set; }
public EnumerationDataProvider()
{
this.MethodName = "GetEnumerations";
this.ObjectType = typeof(EnumerationHelper);
}
protected override void EndInit()
{
this.MethodParameters.Clear();
this.MethodParameters.Add(this.EnumerationType);
this.MethodParameters.Add(this.NoneValue);
this.MethodParameters.Add(this.NoneDisplayValue);
base.EndInit();
}
private static class EnumerationHelper
{
private const string NoneDefaultValue = "None";
private const string NoneDisplayDefaultValue = "";
public static IDictionary<object, string> GetEnumerations(
Type enumType, string noneValue, string noneDisplayValue)
{
// Setup defaults if none exists.
if (string.IsNullOrWhiteSpace(noneValue)) noneValue = NoneDefaultValue;
if (string.IsNullOrWhiteSpace(noneDisplayValue)) noneDisplayValue = NoneDisplayDefaultValue;
IDictionary<object, string> items = new Dictionary<object, string>();
// Find the None enumeration value.
string noneStringValue = (from n in Enum.GetNames(enumType)
where string.Compare(n, noneValue, StringComparison.OrdinalIgnoreCase) == 0
select n).FirstOrDefault();
bool hasNoneValue = !string.IsNullOrWhiteSpace(noneStringValue);
object noneEnumerationValue = null;
if (hasNoneValue)
{
// Parse the None value if it exists and add it as the first item.
noneEnumerationValue = Enum.Parse(enumType, noneStringValue, true);
items.Add(noneEnumerationValue, noneDisplayValue);
}
// Select all enumeration values where not equal to the None value and order by
// the enumeration value name.
var orderedEnums = from v in Enum.GetValues(enumType).Cast<object>()
where !v.Equals(noneEnumerationValue)
orderby Convert.ToString(v) select v;
foreach (var value in orderedEnums)
items.Add(value, Convert.ToString(value));
return items;
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Ecma Script 문법 (2)지난 번에는 es6문법에서 비교적 개념 이해가 쉬우며, 코드 사용법에 대한 변화정도에 대해서 설명했었다. 이번에는 개인적으로 내가 es6문법을 공부할 때 가장 이해하기 어려웠던(지금도 정확하게 이해가 잘 되지 않는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.