我想要一个以货币格式显示数字的TextBox
(通过在绑定上设置StringFormat=c
(。当选择TextBox
时(当IsKeyboardFocused==true
时(,我希望格式化消失,直到失去对TextBox
的关注。
我找到了一种方法,代码粘贴在下面。我的问题是绑定是在Style
中指定的——这意味着我必须为每个想要执行此操作的TextBox
重新键入样式。理想情况下,我想把样式放在中心位置,并为每个TextBox
重用它,每个都有不同的绑定目标。
有没有一种方法可以让我使用Style
在现有绑定上设置一个参数,比如Text.Binding.StringFormat=""
?(与将Text的整个值设置为新定义的Binding相反(
还将感谢为实现这一目标提出的其他建议。
代码(这很有效,只是不方便(:
<TextBox x:Name="ContractAmountTextBox">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<DataTrigger Binding="{Binding IsKeyboardFocused, ElementName=ContractAmountTextBox}" Value="False">
<Setter Property="Text" Value="{Binding Path=ContractAmount, UpdateSourceTrigger=LostFocus, StringFormat=c}"/>
</DataTrigger>
<DataTrigger Binding="{Binding IsKeyboardFocused, ElementName=ContractAmountTextBox}" Value="True">
<Setter Property="Text" Value="{Binding Path=ContractAmount, UpdateSourceTrigger=LostFocus}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</TextBox.Style>
使用附加的属性是可行的,但这意味着必须完全替换绑定,然后将其放回。
这里有一个快速而肮脏的实现:
public static class TextBoxBehavior
{
#region StringFormat
public static string GetStringFormat(TextBox obj)
{
return (string)obj.GetValue(StringFormatProperty);
}
public static void SetStringFormat(TextBox obj, string value)
{
obj.SetValue(StringFormatProperty, value);
}
public static readonly DependencyProperty StringFormatProperty =
DependencyProperty.RegisterAttached(
"StringFormat",
typeof(string),
typeof(TextBoxBehavior),
new UIPropertyMetadata(
null,
StringFormatChanged));
// Used to store the original format
private static readonly DependencyPropertyKey OriginalBindingPropertyKey =
DependencyProperty.RegisterAttachedReadOnly(
"OriginalBinding",
typeof(BindingBase),
typeof(TextBoxBehavior),
new UIPropertyMetadata(null));
private static void StringFormatChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
{
TextBox textBox = o as TextBox;
if (textBox == null)
return;
string oldValue = (string)e.OldValue;
string newValue = (string)e.NewValue;
if (!string.IsNullOrEmpty(oldValue) && string.IsNullOrEmpty(newValue))
{
// Update target for current binding
UpdateTextBindingSource(textBox);
// Restore original binding
var originalBinding = (BindingBase)textBox.GetValue(OriginalBindingPropertyKey.DependencyProperty);
if (originalBinding != null)
BindingOperations.SetBinding(textBox, TextBox.TextProperty, originalBinding);
textBox.SetValue(OriginalBindingPropertyKey, null);
}
else if (!string.IsNullOrEmpty(newValue) && string.IsNullOrEmpty(oldValue))
{
// Get current binding
var originalBinding = BindingOperations.GetBinding(textBox, TextBox.TextProperty);
if (originalBinding != null)
{
// Update target for current binding
UpdateTextBindingSource(textBox);
// Create new binding
var newBinding = CloneBinding(originalBinding);
newBinding.StringFormat = newValue;
// Assign new binding
BindingOperations.SetBinding(textBox, TextBox.TextProperty, newBinding);
// Store original binding
textBox.SetValue(OriginalBindingPropertyKey, originalBinding);
}
}
}
private static void UpdateTextBindingSource(TextBox textBox)
{
var expr = textBox.GetBindingExpression(TextBox.TextProperty);
if (expr != null &&
expr.ParentBinding != null &&
(expr.ParentBinding.Mode == BindingMode.Default // Text binds two-way by default
|| expr.ParentBinding.Mode == BindingMode.TwoWay
|| expr.ParentBinding.Mode == BindingMode.OneWayToSource))
{
expr.UpdateSource();
}
}
private static Binding CloneBinding(Binding original)
{
var copy = new Binding
{
Path = original.Path,
XPath = original.XPath,
Mode = original.Mode,
Converter = original.Converter,
ConverterCulture = original.ConverterCulture,
ConverterParameter = original.ConverterParameter,
FallbackValue = original.FallbackValue,
TargetNullValue = original.TargetNullValue,
NotifyOnSourceUpdated = original.NotifyOnSourceUpdated,
NotifyOnTargetUpdated = original.NotifyOnTargetUpdated,
NotifyOnValidationError = original.NotifyOnValidationError,
UpdateSourceExceptionFilter = original.UpdateSourceExceptionFilter,
UpdateSourceTrigger = original.UpdateSourceTrigger,
ValidatesOnDataErrors = original.ValidatesOnDataErrors,
ValidatesOnExceptions = original.ValidatesOnExceptions,
BindingGroupName = original.BindingGroupName,
BindsDirectlyToSource = original.BindsDirectlyToSource,
AsyncState = original.AsyncState,
IsAsync = original.IsAsync,
StringFormat = original.StringFormat
};
if (original.Source != null)
copy.Source = original.Source;
if (original.RelativeSource != null)
copy.RelativeSource = original.RelativeSource;
if (original.ElementName != null)
copy.ElementName = original.ElementName;
foreach (var rule in original.ValidationRules)
{
copy.ValidationRules.Add(rule);
}
return copy;
}
#endregion
}
用法:
<TextBox x:Name="ContractAmountTextBox"
Text="{Binding Path=ContractAmount, UpdateSourceTrigger=LostFocus, StringFormat=c}">
<TextBox.Style>
<Style TargetType="{x:Type TextBox}">
<Style.Triggers>
<Trigger Property="IsKeyboardFocused" Value="True">
<Setter Property="local:TextBoxBehavior.StringFormat" Value="N"/>
</Trigger>
</Style.Triggers>
</Style>
</TextBox.Style>
</TextBox>
使用它,您还可以为不同的TextBoxes重新使用样式