通过使用样式修改文本框文本绑定的参数

2023-11-29

我想要一个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
}

Usage:

<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>

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

通过使用样式修改文本框文本绑定的参数 的相关文章

  • 将 pandas DataFrame 中的数字转换为特定字符串格式

    我需要运行一个可以通过循环完成的任务 但我想有一种更有效 更漂亮的方法来做到这一点 我有一个DataFrame它有一个整数列 我想将其转换为 4 位字符串表示形式 也就是说 3 应转换为 0003 234 应转换为 0234 我正在寻找一种
  • WPF;单击 ListView 中的空白区域时如何取消选择所有选定的项目

    当我有几个 甚至一个 selected items我按简单click在我的空白处ListView 空白 非行 我想取消选择所有选定的项目 这是我取消选择所有项目的功能 private void DeselectAllListViewItem
  • 控制不同的DataContext

    控制WPF中不同的DataContext 因为我可以在不同的选项卡中部署多个 DataContext 并控制当前的 DataContext 我在用着Mvvm Light WPF4我有不同的 ViewModels View 但我不知道如何处理
  • 如何在MVVM中实现对话框架构

    我正在开发一个基于 PRISM 框架 Unity Container 的 WPF 4 0 MVVM 应用程序 我想知道在 mvvm 模式中实现对话框的最佳方法是什么 我计划在我的应用程序中使用相当多的东西 所以我想要一些可重用的东西 由于您
  • WPF DataGrid 绑定 DataGridCell 内容

    希望这将是一个非常简单的答案 我认为我只是没有看到众所周知的树木 我有一个 DataGridCell 样式 我想将单元格的内容绑定到图像的源属性 这是我目前使用的 XAML
  • 在 win 10 通用应用程序中使用 mvvm-light 导航服务时,无法将页面类型的对象转换为“Windows.UI.Xaml.Controls.Frame”

    我在新的 Windows 10 通用应用程序 C XAML 上遇到以下错误 GalaSoft MvvmLight Platform dll 中发生 System InvalidCastException 类型的异常 但未在用户代码中处理 其
  • 滚动 X 轴绘图区域 - Silverlight 柱系列

    我有一个工作正常的柱形系列图表 我有一个需要添加的功能 我希望水平滚动能够启用到 x 轴的绘图区域 这是屏幕截图 如果您看到屏幕截图 我有 6 个项目 并且由于项目数量较多 条形图非常细 所以假设如果我有 20 个项目 那么条形图将根本不可
  • 获取代码中的绑定结果

    我可能正在以错误的方式寻找这个 但是 有没有办法通过代码获取绑定的结果值 可能是一些显而易见的东西 但我就是找不到它 您只需致电ProvideValue的绑定方法 困难的部分是你需要通过有效的IServiceProvider到方法 编辑 实
  • 无法使用 DialogResult

    我尝试使用DialogResult检查一个Messagebox s 是 否 取消 我正在使用以下代码 我没有看到任何问题 DialogResult dlgResult MessageBox Show Save changes before
  • 复选框上的数据绑定

    我目前正在将数据从 SQL 数据库之一提取到我的应用程序中 我可以让它适用于我的文本框和其他项目 但是 我似乎无法让它适用于复选框 这是我正在使用的代码 DataTable dt new DataTable dt using SqlConn
  • 使 DataTemplate 可混合

    如何为 ViewModel 制作可混合的数据模板 可在表达式混合中设计 当我转到资源并尝试直接编辑数据模板时 我在绘图板上看到的只是一个空白矩形 这是因为 DataTemplate 没有绑定到任何东西 当然 我可以创建一个 UserCont
  • 根据属性的类型使用文本框或复选框

    如果我有这样的结构 public class Parent public string Name get set public List
  • VisualStateManager 和生成的转换

    正当我以为我明白的时候VisualStateManager 有些事情证明我错了 我正在使用 WPF 4 并且尝试简单地在鼠标悬停时放大某个项目 并在鼠标离开时将其缩小 我想我只需定义每个状态VisualStateGroup然后指定一个Vis
  • 如何在C#背后的代码中动态创建数据模板并绑定TreeView分层数据

    我有一个场景 其中树视图动态更改其数据模板和数据绑定定义 我在 XAML 中创建了一个树视图 如下所示
  • Python str.format() 方法的默认 kwarg 值

    我希望尝试使现有字符串的复数化尽可能简单 并且想知道是否有可能得到str format 在查找 kwargs 时解释默认值 这是一个例子 string number of sheep sheep has run away dict comp
  • 先学Silverlight还是先学WPF?

    看来 Silverlight WPF 是 NET 用户界面开发的长期未来 这很棒 因为我可以看到在客户端和 Web 开发端重用 XAML 技能的优势 但看看 WPF XAML Silverlight 它们似乎是非常庞大的技术 那么从哪里开始
  • 如何在运行时添加到 TreeView 目录

    我有一个TreeView我想允许用户添加和删除子项目 在探索基本功能时 我使用button and a textbox添加此子项 当用户点击button a new TreeViewItem需要创建并设置为我的父项的子项TreeView与t
  • WPF 处理文本、图像和文件粘贴事件

    我正在开发一个 WPF 应用程序 我想捕获 RichTextBox 输入中的粘贴命令并处理粘贴的文件 为此 我使用以下回调
  • 不支持将数据直接绑定到存储查询(DbSet、DbQuery、DbSqlQuery)

    正在编码视觉工作室2012并使用实体模型作为我的数据层 但是 当页面尝试加载时 上面提到的标题 我使用 Linq 语句的下拉控件往往会引发未处理的异常 下面是我的代码 using AdventureWorksEntities dw new
  • WPF/C# 将自定义对象列表数据绑定到列表框?

    我在将自定义对象列表的数据绑定到ListBox in WPF 这是自定义对象 public class FileItem public string Name get set public string Path get set 这是列表

随机推荐