如何更改(即切换)WPF控件的绑定StringFormat属性? [英] How can I change (i.e. toggle) the bound, StringFormat property of a WPF control?

查看:157
本文介绍了如何更改(即切换)WPF控件的绑定StringFormat属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF TextBox ,它的文本值绑定在XAML中。可以正常工作,并且按预期方式,将关联值加载到字段中时,可以按照期望的格式对其进行格式化。

I have a WPF TextBox that has it's text value bound in XAML. This works fine and, as expected, when the associated value is loaded to the field, it is formatted as desired.

我的问题是用户在输入文本字段时修改值,我要删除格式,并显示原始基础值。

My issue is when the user enters the text field to modify the value, I want to strip away the formatting, and display the raw underlying value.

为此,我尝试设置 BindingExpression .ParentBinding.StringFormat 属性,位于 GotFocus 事件内绑定的文本框上。但是,当我尝试将 StringFormat 属性设置为空字符串时,出现以下异常:

To do this, I tried to set the BindingExpression.ParentBinding.StringFormat property, on the text boxes binding from within a GotFocus event. However, when I tried to set the StringFormat property to an empty string, I got the following exception:


绑定已使用后无法更改。

Binding cannot be changed after it has been used.

因此,将控件绑定到数据后模型对象,是否可以直接修改 TextBox 的字符串格式?我愿意更改格式化控件值的方式,但最终的愿望是在编辑数据时删除数据的格式,并在用户退出字段后重新设置字符串格式。

So, after a control has been bound to a data model object, is there a straight-forward way that I can modify the string format of the TextBox? I'm willing to change how I format the value of the control, but the end desire is to strip the formatting of the data when it is being edited, and re-instating the string formatting once the user exits the field.

推荐答案

我可能会尝试不同的方法。听起来您正在使用相同的TextBox作为格式化值并编辑原始值。使用两个不同的TextBox进行查看和编辑,或将其打包在 UserControl 中会更干净。另外,我将使用两个属性,例如 RawText FormattedText ,以及 UserControl 将具有DependencyProperties与绑定到两个属性。 UserControl 将自动切换到编辑文本框。但是,如何自动切换的问题可能是一个挑战。您可能需要使用您提到的View TextBox的GotFocus,尽管拥有一个真正的 Edit 按钮可以为您切换按钮并不是一个坏主意。

I would probably try it differently. It sounds like you are using the same TextBox for the formatted value and editing the raw value. It would be cleaner to use two different TextBoxes for View and Edit, or package it in a UserControl. Also, I would use two properties, e.g. RawText and FormattedText, and the UserControl would have DependencyProperties with bindings to both properties. The UserControl would automatically switch to the Edit TextBox. The question of "how does the automatic switching" work may be a challenge though. You probably need to use the GotFocus of the View TextBox as you mentioned, although it might not be a bad idea to have an actual Edit button that switches it for you.

我了解切换到WPF的知识。切换到WPF时需要进行很多调整(又称学习)。我认为这是设计一种适合目的的表单或控件。不过,您不必创建新的UserControl。您可以执行类似于StackOverflow的操作,其中有一个Edit TextBox,然后是View区域,这等效于Raw和Formatted值。您可以通过 BoolToVisibilityConverter 处于编辑模式时。在ViewModel上创建公共布尔IsEditing 属性,并将其绑定到< EditTextBox Visibility = {Binding IsEditing,Converter = {StaticResource BoolToVisibilityConverter}} Text = {Binding RawText} ... etc ... /> 使用WPF一段时间后,您真的很欣赏数据绑定,这使得它很难回到普通的WinForms (至少对我来说-并不是说没有挑战!)。

I understand about switching to WPF. There is quite a bit of adjustment (aka learning) when switching to WPF. I would think of it as designing a form or control that is "fit for purpose". You don't have to create a new UserControl though. You could do something similar to StackOverflow where there is an Edit TextBox and then the View area, that would be equivalent to the Raw and Formatted values. You would control the Visibility of the Edit TextBox through a BoolToVisibilityConverter when you are in edit mode. Create a public bool IsEditing property on your ViewModel and bind that to the <EditTextBox Visibility="{Binding IsEditing, Converter={StaticResource BoolToVisibilityConverter}}" Text="{Binding RawText}" ...etc... /> After working with WPF for a while, you really appreciate data binding and it makes it hard to go back to plain WinForms (at least for me - not to say there aren't challenges though!).

这篇关于如何更改(即切换)WPF控件的绑定StringFormat属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆