이 코드는 동안에는은 formatString 다시 변환 할 수있는 상태에서 소스 속성 있으며, toString() 버전을 떠날 때, 텍스트 상자 또는 유사한 컨트롤에 바인딩 두 가지 방법을 사용할 수 있습니다. "1,234.56"을 구문 분석 할 수 있기 때문에 "#, 0.00"과 같은 형식이 좋지만 FormatString = "일부 접두사 텍스트 #, 0.00"은 다시 구문 분석 할 수없는 "일부 접두어 텍스트 1,234.56"으로 변환됩니다.
XAML : 소스 속성이 null이 될 수있는 경우
<TextBox>
<TextBox.Text>
<MultiBinding Converter="{StaticResource ToStringFormatConverter}"
ValidatesOnDataErrors="True" NotifyOnValidationError="True" TargetNullValue="">
<Binding Path="Property" TargetNullValue="" />
<Binding Path="PropertyStringFormat" Mode="OneWay" />
</MultiBinding>
</TextBox.Text>
</TextBox>
참고 TargetNullValue 중복.
C 번호 :
좋은 제안입니다
/// <summary>
/// Allow a binding where the StringFormat is also bound to a property (and can vary).
/// </summary>
public class ToStringFormatConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values.Length == 1)
return System.Convert.ChangeType(values[0], targetType, culture);
if (values.Length >= 2 && values[0] is IFormattable)
return (values[0] as IFormattable).ToString((string)values[1], culture);
return null;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
var targetType = targetTypes[0];
var nullableUnderlyingType = Nullable.GetUnderlyingType(targetType);
if (nullableUnderlyingType != null) {
if (value == null)
return new[] { (object)null };
targetType = nullableUnderlyingType;
}
try {
object parsedValue = ToStringFormatConverter.TryParse(value, targetType, culture);
return parsedValue != DependencyProperty.UnsetValue
? new[] { parsedValue }
: new[] { System.Convert.ChangeType(value, targetType, culture) };
} catch {
return null;
}
}
// Some types have Parse methods that are more successful than their type converters at converting strings
private static object TryParse(object value, Type targetType, CultureInfo culture)
{
object result = DependencyProperty.UnsetValue;
string stringValue = value as string;
if (stringValue != null) {
try {
MethodInfo mi;
if (culture != null
&& (mi = targetType.GetMethod("Parse",
BindingFlags.Public | BindingFlags.Static, null,
new[] { typeof(string), typeof(NumberStyles), typeof(IFormatProvider) }, null))
!= null) {
result = mi.Invoke(null, new object[] { stringValue, NumberStyles.Any, culture });
}
else if (culture != null
&& (mi = targetType.GetMethod("Parse",
BindingFlags.Public | BindingFlags.Static, null,
new[] { typeof(string), typeof(IFormatProvider) }, null))
!= null) {
result = mi.Invoke(null, new object[] { stringValue, culture });
}
else if ((mi = targetType.GetMethod("Parse",
BindingFlags.Public | BindingFlags.Static, null,
new[] { typeof(string) }, null))
!= null) {
result = mi.Invoke(null, new object[] { stringValue });
}
} catch (TargetInvocationException) {
}
}
return result;
}
}
. 나는 이것을 조사해야 할 것이다. 나는 사용자 정의 컨트롤을 포함하지 않는 솔루션이있을 것이라고 기대했지만, 분명히 열려 있습니다. 나는 약간의 연구 끝에 다시 확인해 볼 것입니다. –
나는 똑같은 일을하려하지만,이를 처리하기 위해 첨부 된 속성을 설정하는 방법을 모르겠습니다. 나는 새로운 질문을 게시했다. http://stackoverflow.com/q/24119097/65461 –