当前位置: 首页 > news >正文

娄底建网站唐山seo

娄底建网站,唐山seo,哈尔滨建设局官网,四平网站建设404页面对网站的好处及设置方法在WPF Samples中有一个关于数据绑定到方法的Demo&#xff0c;该Demo结构如下&#xff1a; 运行效果如下所示&#xff1a; 来看看是如何实现的。 先来看下MainWindow.xaml中的内容&#xff1a; <Window.Resources><ObjectDataProvider ObjectType"{x:Type local…

在WPF Samples中有一个关于数据绑定到方法的Demo,该Demo结构如下:

image-20240621101313200

运行效果如下所示:

来看看是如何实现的。

先来看下MainWindow.xaml中的内容:

<Window.Resources><ObjectDataProvider ObjectType="{x:Type local:TemperatureScale}"MethodName="ConvertTemp" x:Key="ConvertTemp"><ObjectDataProvider.MethodParameters><system:Double>0</system:Double><local:TempType>Celsius</local:TempType></ObjectDataProvider.MethodParameters></ObjectDataProvider><local:DoubleToString x:Key="DoubleToString" /></Window.Resources>

在窗体资源中定义了一个ObjectDataProvider对象与DoubleToString对象。

ObjectDataProvider介绍

image-20240621102324090

ObjectDataProvider类用于包装和创建可以用作绑定源的对象。

 <ObjectDataProvider MethodName="ConvertTemp" >

image-20240621102953193

ObjectDataProvider.MethodName 属性获取或设置要调用的方法的名称。

<ObjectDataProvider.MethodParameters><system:Double>0</system:Double><local:TempType>Celsius</local:TempType>
</ObjectDataProvider.MethodParameters>

image-20240621103147579

ObjectDataProvider.MethodParameters属性获取要传递给方法的参数列表。

绑定到方法

<TextBox Grid.Row="1" Grid.Column="1" Name="tb"><TextBox.Text><Binding Source="{StaticResource ConvertTemp}" Path="MethodParameters[0]"BindsDirectlyToSource="true" UpdateSourceTrigger="PropertyChanged"Converter="{StaticResource DoubleToString}"><Binding.ValidationRules><local:InvalidCharacterRule/></Binding.ValidationRules></Binding></TextBox.Text>
</TextBox>

绑定路径为方法的第一个参数。

使用的转换器是在资源中定义的DoubleToString

BindsDirectlyToSource属性设置为true,表示直接绑定到数据源对象,而不是是绑定到数据源对象的某个属性。

<Binding.ValidationRules><local:InvalidCharacterRule/>
</Binding.ValidationRules>

image-20240621104346213

Binding.ValidationRules属性用于定义数据绑定的验证规则。这些规则用于在数据绑定过程中验证输入数据的有效性。如果数据不符合验证规则,WPF 可以显示错误信息或采取其他措施来处理无效数据。

这里使用的是InvalidCharacterRule对象。

<ComboBox Grid.Row="1" Grid.Column="2" 
SelectedValue="{Binding Source={StaticResource ConvertTemp},
Path=MethodParameters[1], BindsDirectlyToSource=true}"><local:TempType>Celsius</local:TempType><local:TempType>Fahrenheit</local:TempType>
</ComboBox>

ComboBox绑定到方法的第二个参数。

<Label Content="{Binding Source={StaticResource ConvertTemp}}"
Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"/>

绑定到方法的返回值。

现在查看这个方法:

  public string ConvertTemp(double degree, TempType temptype){Type = temptype;switch (temptype){case TempType.Celsius:return (degree*9/5 + 32).ToString(CultureInfo.InvariantCulture) + " " + "Fahrenheit";case TempType.Fahrenheit:return ((degree - 32)/9*5).ToString(CultureInfo.InvariantCulture) + " " + "Celsius";}return "Unknown Type";}

绑定过程梳理

该Demo启动后的界面如下所示:

image-20240621105239909

 <ObjectDataProvider ObjectType="{x:Type local:TemperatureScale}"MethodName="ConvertTemp" x:Key="ConvertTemp"><ObjectDataProvider.MethodParameters><system:Double>0</system:Double><local:TempType>Celsius</local:TempType></ObjectDataProvider.MethodParameters></ObjectDataProvider>

ObjectDataProvider.MethodParameters中设置了方法的参数为0和Celsius。

而0是Double类型,TextBox需要string类型,因此设置了DoubleToString转换器。

该转换器代码如下所示:

 public class DoubleToString : IValueConverter{public object Convert(object value, Type targetType, object parameter,CultureInfo culture) => value?.ToString();public object ConvertBack(object value, Type targetType, object parameter,CultureInfo culture){var strValue = value as string;if (strValue != null){double result;var converted = double.TryParse(strValue, out result);if (converted){return result;}}return null;}}
<ComboBox Grid.Row="1" Grid.Column="2" 
SelectedValue="{Binding Source={StaticResource ConvertTemp},
Path=MethodParameters[1], BindsDirectlyToSource=true}"><local:TempType>Celsius</local:TempType><local:TempType>Fahrenheit</local:TempType>
</ComboBox>

ComboBox的SelectedValue绑定到方法的第二个参数,刚开始也就是Celsius。

<Label Content="{Binding Source={StaticResource ConvertTemp}}"
Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"/>

绑定到方法的返回值。

在TextBox中输入了做了数据绑定的验证规则。

InvalidCharacterRule类代码如下所示:

internal class InvalidCharacterRule : ValidationRule
{public override ValidationResult Validate(object value, CultureInfo cultureInfo){var myvalue = 0.00;try{if (((string) value).Length > 0)myvalue = double.Parse((string) value);}catch (Exception e){return new ValidationResult(false, "Illegal characters or " + e.Message);}return new ValidationResult(true, null);}
}

当输入不符合规则时,会有提示,如下图所示:

image-20240621110313599

代码来源

[WPF-Samples/Data Binding/BindingToMethod at main · microsoft/WPF-Samples (github.com)](https://github.com/microsoft/WPF-Samples/tree/main/Data Binding/BindingToMethod)

http://www.hkea.cn/news/153701/

相关文章:

  • 电子商务网站开发进什么科目模板自助建站
  • 威海市住房和城乡建设局官方网站北京seo营销公司
  • 开网页卡优化关键词排名工具
  • wordpress右侧文章归档东莞公司seo优化
  • 个人网站建设需求说明书免费外链生成器
  • 湖南网站建设的公司排名网页制作网站制作
  • 公司网页网站建设 ppt模板app开发公司排行榜
  • 网站开发yuanmus联合早报 即时消息
  • 为什么只有中国人怕疫情seo 页面
  • 网站开发总结报告十大门户网站
  • 临淄信息港发布信息临沂seo建站
  • 门户网站系统介绍企业推广哪个平台好
  • 免费网站建站排行榜网站策划报告
  • 网站设计的评估最近发生的热点新闻
  • 设建网站广告投放渠道
  • 日ip5000的网站怎么做如何提高网站在搜索引擎中的排名
  • 网站描文本链接怎么做深圳互联网营销
  • 一个服务器做两个网站自己做网站
  • 百草味网站建设的活动方案营销型企业网站有哪些
  • 论文课程网站 建设背景项目推广方式有哪些
  • 内部网站建设关键词优化推广策略
  • 一个公司可以做几个网站备案贵阳网络推广排名
  • 武汉高端网站建设免费广告网
  • 大理建网站常用于网站推广的营销手段是
  • js怎么做网站跨境电商网站
  • 台州外贸网站建设百度推广费用多少
  • 虚拟机怎么做网站空间培训班管理系统 免费
  • wordpress离线文章发布郑州seo关键词排名优化
  • 龙岗区网站建设中国职业培训在线
  • 南山网站建设外包优化网站