Dreamine.UI.Wpf 1.0.1
Dreamine.UI.Wpf 사용자 인터페이스 기능과 구성 요소를 제공합니다.
로딩중...
검색중...
일치하는것 없음
DoubleEqualsConverter.cs
이 파일의 문서화 페이지로 가기
1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using System.Windows.Data;
8
10{
19 public class DoubleEqualsConverter : IValueConverter
20 {
29 public string Format { get; set; } = "0.000";
30
39 public double? Min { get; set; }
40
49 public double? Max { get; set; }
50
59 public bool AllowEmpty { get; set; } = true;
60
117 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
118 {
119 if (value is null) return AllowEmpty ? string.Empty : (0d).ToString(Format, culture);
120 if (value is double d)
121 {
122 if (double.IsNaN(d) || double.IsInfinity(d))
123 return AllowEmpty ? string.Empty : (0d).ToString(Format, culture);
124 return d.ToString(Format, culture);
125 }
126 // 그 외 수치형
127 if (value is IFormattable f)
128 return f.ToString(Format, culture);
129
130 // 마지막 보루: 변환 시도
131 try
132 {
133 var dv = System.Convert.ToDouble(value, CultureInfo.InvariantCulture);
134 return dv.ToString(Format, culture);
135 }
136 catch
137 {
138 return AllowEmpty ? string.Empty : (0d).ToString(Format, culture);
139 }
140 }
141
190 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
191 {
192 var s = value as string ?? string.Empty;
193 s = s.Trim();
194
195 // 빈 문자열 → 원본 유지(또는 0으로 초기화하고 싶으면 여기서 0d 반환)
196 if (string.IsNullOrEmpty(s))
197 return AllowEmpty ? Binding.DoNothing : 0d;
198
199 // 사용자가 아직 타이핑 중인 중간 형태면 원본 보존
200 var dec = culture.NumberFormat.NumberDecimalSeparator;
201 if (s == "-" || s.EndsWith(dec, StringComparison.Ordinal))
202 return Binding.DoNothing;
203
204 // 문화권 기반 파싱
205 if (double.TryParse(s, NumberStyles.Float | NumberStyles.AllowThousands, culture, out var d))
206 {
207 if (Min.HasValue && d < Min.Value) d = Min.Value;
208 if (Max.HasValue && d > Max.Value) d = Max.Value;
209 return d;
210 }
211
212 // 파싱 실패 → 원본 값 보존
213 return Binding.DoNothing;
214 }
215 }
216
225 public class DoubleEqualsBoolConverter : IValueConverter
226 {
235 public double Epsilon { get; set; } = 1e-6;
236
285 public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
286 {
287 if (value is double v && TryParseParam(parameter, out var p))
288 return Math.Abs(v - p) < Epsilon;
289 return false;
290 }
291
340 public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
341 {
342 if (value is bool b && b && TryParseParam(parameter, out var p))
343 return p;
344 return Binding.DoNothing;
345 }
346
379 private static bool TryParseParam(object parameter, out double result)
380 {
381 var s = parameter?.ToString() ?? "";
382 // 파라미터는 일반적으로 상수 문자열이므로 InvariantCulture로 파싱
383 return double.TryParse(s, NumberStyles.Float, CultureInfo.InvariantCulture, out result);
384 }
385 }
386
387
388}
object Convert(object value, Type targetType, object parameter, CultureInfo culture)
object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
static bool TryParseParam(object parameter, out double result)
object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
object Convert(object value, Type targetType, object parameter, CultureInfo culture)