In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
本篇内容介绍了"Blazor数据绑定的方法是什么"的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
Blazor当中, 类似实现了WPF的基础绑定功能, 支持在HTML元素当中使用Razor语法进行绑定C#字段、属性或 Razor 表达式值。
绑定语法
在Html标签中,使用Razor的语法, 前缀使用@符号, 然后添加 @bind="xxxx" 即可实现最简单的绑定, 如下:
input @bind="Name" />@code { private string Name{ get; set; }}
通过上面的操作, Input中的值与属性Name则进行了一个双向绑定, 无论是Name主动发生变更, 还是Input的值发生变化, 两者都会同时更新。
通过@bind的声明与 value="@xxx"的区别:
@xxx/@bind区别
value="@xxx": 只能做到属性呈现到UI元素当中, 无法与UI元素建立关系, 即: 元素发生变化并不会影响到属性变更。
绑定对象
下面示例演示了一个如何在html元素中的值如何绑定到一个对象的属性上。
@code{ public Student Stu { get; set; } = new Student() { Name = "123" }; public class Student { public string Name { get; set; } }}
效果如下所示:
数据格式化
如果需要使用数据格式化显示, 可以通过Razor中的语法: @bind:format , 如下所示,展示了一个日期元素格式化样例:
@code { [Parameter] public DateTime StartDate { get; set; } = new DateTime(2020, 1, 1);}绑定子组件属性
通常情况下, 对于父界面而言, 往往是由一个或者多个子组件组成 (用于提高程序的复用性), 所以父组件而言, 往往需要将父组件的参数绑定到子组件当中。
下面的例子,演示了一个子组件当中的字符串, 如何绑定到父组件的属性上。
声明子组件参数与回调函数
声明了组件参数Year, 还需要声明一个组件回调函数, 该回调函数主要用于子组件的数据发生变更。
回调函数的声明规则为, 参数类型与对应的属性一直, 名称以属性名开头
Year: @Year
@code { [Parameter] public int Year { get; set; } [Parameter] public EventCallback YearChanged { get; set; }}父组件调用@code { [Parameter] public int ParentYear { get; set; } = 1978;}无法预估的值
当元素与属性建立了绑定关系, 但是受限于属性的类型, 如果UI元素中发生了类型不一致的变更,则在触发绑定事件时,无法分析的值会自动还原为以前的值。
例: 当绑定的属性为Int类型时, UI元素将内容修改为string字符, 当UI元素发生事件变更时, 该值无法进行int类型的转换,则回自动还原。
"Blazor数据绑定的方法是什么"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.