wpf - 通过使用样式修改文本框文本绑定(bind)的参数

标签 wpf xaml data-binding styles string-formatting

我想要一个以货币格式显示数字的 TextBox(通过在绑定(bind)上设置 StringFormat=c)。选择 TextBox 时(当 IsKeyboardFocused==true 时),我希望格式消失,直到焦点在 TextBox 上迷路了。

我找到了一种方法来做到这一点,代码粘贴在下面。我的问题是,绑定(bind)是在 Style 中指定的 - 这意味着我必须为我想要为其执行此操作的每个 TextBox 重新键入样式。理想情况下,我想将样式放在中心位置,并为每个 TextBox 重用它,每个都有不同的绑定(bind)目标。

我有没有办法使用 Style 在现有绑定(bind)上设置参数,比如 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>

最佳答案

附加属性是可行的,但这意味着您必须完全替换绑定(bind),然后再放回去。

这是一个快速而肮脏的实现:

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>

使用它,您还可以为不同的文本框重用样式

关于wpf - 通过使用样式修改文本框文本绑定(bind)的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5594437/

相关文章:

c# - 在 wpf/C# 中绑定(bind)

c# - WPF 依赖属性 - 数据绑定(bind)不起作用

wpf - 使用 BackgroundWorker 更新 GUI

c# - 如何在 ItemsSource = 自定义对象集合时选择/展开 TreeView 节点?

c# - 将 Treeviews 放在 TabControl 内容页面中

wpf - 在 MouseOver 上更改用作按钮模板的路径的填充颜色

c# - 如何将App.XAML样式应用到当前窗口

WPF数据绑定(bind)计时问题

wpf - 仅将默认 ControlTemplate 应用于一个控件

wpf - Entity Framework 在克隆后附加异常