廊坊怎么做网站,广东十大广告公司,设计师经常上的网站,海南网页设计培训在数据绑定#xff08;Data Binding#xff09;的上下文中#xff0c;我们经常使用继承 IValueConverter 接口的类#xff0c;用于在源值和目标值之间进行转换。该接口定义了两个方法#xff1a;Convert 和 ConvertBack#xff0c;这两个方法分别用于从源值到目标值的转换… 在数据绑定Data Binding的上下文中我们经常使用继承 IValueConverter 接口的类用于在源值和目标值之间进行转换。该接口定义了两个方法Convert 和 ConvertBack这两个方法分别用于从源值到目标值的转换和从目标值回到源值的转换如果绑定支持双向绑定。 TextBox 控件的 UpdateSourceTrigger 属性用于控制何时更新绑定源的值它有以下4个可选值
PropertyChanged当 TextBox 的内容发生任何变化时都会更新绑定源。这是默认值提供了即时反馈但可能会增加应用程序的负载因为每次击键都会触发更新。主要用于实时校验文本输入。LostFocus当 TextBox 失去焦点时更新绑定源。这减少了不必要的更新但可能意味着在用户完成输入之前绑定源的值不会反映最新的更改。Explicit不会自动更新绑定源。你需要显式调用 BindingExpression 的 UpdateSource 方法来更新绑定源。Default使用绑定目标的默认 UpdateSourceTrigger 值。对于 TextBox这通常是 PropertyChanged。 TextBox 控件的绑定模式Mode通常不是 TextBox 控件本身的直接属性而是与数据绑定Data Binding相关的属性。当在 TextBox 的 Text 属性上使用数据绑定时有时需要设置 Binding 对象的 Mode 属性来控制数据的流动方向。它有如下5个可选值 OneWay这是单向绑定的默认模式。当源属性的值改变时目标属性即 TextBox 的 Text 属性的值会自动更新。但是对目标属性的更改不会传播回源属性。 TwoWay这是双向绑定的模式。当源属性或目标属性的值改变时另一个属性的值也会自动更新。在 TextBox 的上下文中这意味着当你在文本框中输入文本时绑定的源属性会更新同样地如果源属性的值在代码中被改变文本框中的内容也会更新。 OneWayToSource这是另一种单向绑定模式但与 OneWay 相反。在这种模式下当目标属性TextBox 的 Text 属性的值改变时源属性的值也会更新但源属性的变化不会影响到目标属性。 OneTime在这种模式下数据只在绑定时从源传输到目标一次。之后源属性的变化不会反映到目标属性上并且目标属性的变化也不会尝试回写到源属性。 Default这表示使用绑定目标的默认模式。对于 TextBox 的 Text 属性默认模式通常是 TwoWay但这也可能取决于具体的绑定上下文和父控件的设置。 如下xaml页面中左边的TextBox使用Converter属性绑定值转换器右边的则没有。
Window x:ClassTextConverterTest.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:TextConverterTestmc:IgnorabledTitleMainWindow Height450 Width800Window.Resourceslocal:NumericToStringConverter x:KeyNToSConverter/Style TargetTypeTextBoxSetter PropertyHeight Value80/SetterSetter PropertyWidth Value180/SetterSetter PropertyHorizontalContentAlignment ValueCenter/SetterSetter PropertyVerticalContentAlignment ValueCenter/SetterSetter PropertyFontSize Value20/Setter/Style/Window.ResourcesGrid Height100 Width400Grid.RowDefinitionsRowDefinition/RowDefinition/Grid.RowDefinitionsGrid.ColumnDefinitionsColumnDefinition/ColumnDefinitionColumnDefinition/ColumnDefinition/Grid.ColumnDefinitionsTextBox Grid.Row0 Grid.Column0 Text{Binding PathTextBoxLeftStr, ModeTwoWay, UpdateSourceTriggerPropertyChanged, Converter{StaticResource NToSConverter}}/TextBoxTextBox Grid.Row0 Grid.Column1 Text{Binding PathTextBoxRightStr, ModeTwoWay}, UpdateSourceTriggerPropertyChanged/TextBox/Grid
/Window添加TextBox的Path绑定的字段属性。在构造函数中必须要有【DataContext this;】这行代码用于获取或设置元素参与数据绑定时的数据上下文。
/// summary
/// Interaction logic for MainWindow.xaml
/// /summary
public partial class MainWindow : Window
{public string TextBoxLeftStr { get; set; }public string TextBoxRightStr { get; set; }public MainWindow(){InitializeComponent();DataContext this;//必须要有关联上下文}
} IValueConverter接口的具体实现
public class NumericToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{try{if (value null || string.IsNullOrEmpty(value.ToString())){return ;}else if (value.ToString().Last() .){return value.ToString();}decimal decimalValue System.Convert.ToDecimal(value);string format decimalValue % 1 0 ? 0 : 0.0; // 如果小数部分为0则不显示小数否则显示一位小数 return decimalValue.ToString(format, CultureInfo.InvariantCulture);}catch (Exception ex){MessageBox.Show(ex.Message);return ;}
}// ConvertBack方法可能不需要因为通常TextBox到数值的转换是单向的
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{try{if (value null || string.IsNullOrEmpty(value.ToString())){return ;}else if (value.ToString().Last() .){return value.ToString();}decimal decimalValue System.Convert.ToDecimal(value);string format decimalValue % 1 0 ? 0 : 0.0; // 如果小数部分为0则不显示小数否则显示一位小数 return decimalValue.ToString(format, CultureInfo.InvariantCulture);}catch (Exception ex){MessageBox.Show(ex.Message);return ;}
}
} 在xaml中引入资源类
Window.Resourceslocal:NumericToStringConverter x:KeyNToSConverter/
/Window.Resources 实现效果左TextBox最多只能输入1位小数。且左右两个TextBox均不能输入除数字和小数点以外的字符。