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

企业网站制作免费下载域名交易网站哪个好

企业网站制作免费下载,域名交易网站哪个好,重庆网站设计制作网站,themegallery模板网WPF 核心概念详解:DataBinding、Dependency Property 和 DataTemplate 1. DataBinding (数据绑定) 基本概念 DataBinding 是 WPF 的核心机制,用于在 UI 元素和数据源之间建立自动同步关系。 关键特性 双向绑定:数据变化自动反映到 UI&…

WPF 核心概念详解:DataBinding、Dependency Property 和 DataTemplate

1. DataBinding (数据绑定)

基本概念

DataBinding 是 WPF 的核心机制,用于在 UI 元素和数据源之间建立自动同步关系。

关键特性

  • 双向绑定:数据变化自动反映到 UI,UI 变化也能更新数据源

  • 绑定模式

    • OneWay:源→目标

    • TwoWay:源↔目标

    • OneWayToSource:目标→源

    • OneTime:仅初始化时绑定一次

基本语法

<TextBox Text="{Binding Path=UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>

代码示例

public class User : INotifyPropertyChanged
{private string _name;public string Name{get => _name;set { _name = value; OnPropertyChanged(); }}public event PropertyChangedEventHandler PropertyChanged;protected void OnPropertyChanged([CallerMemberName] string name = null){PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));}
}// 在窗口或控件中设置数据上下文
this.DataContext = new User { Name = "Alice" };

2. Dependency Property (依赖属性)

基本概念

依赖属性是 WPF 特有的属性系统,支持:

  • 样式设置

  • 数据绑定

  • 动画

  • 属性值继承

  • 默认值和元数据

创建依赖属性

public class MyControl : Control
{public static readonly DependencyProperty ValueProperty =DependencyProperty.Register("Value",                     // 属性名称typeof(int),                 // 属性类型typeof(MyControl),           // 所有者类型new PropertyMetadata(0, OnValueChanged)); // 元数据public int Value{get { return (int)GetValue(ValueProperty); }set { SetValue(ValueProperty, value); }}private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e){// 属性变化时的处理逻辑}
}

依赖属性优势

  1. 内存效率:只有被修改的属性才存储实际值

  2. 内置变更通知:无需实现 INotifyPropertyChanged

  3. 属性值继承:如字体设置可沿可视化树继承

  4. 样式和模板支持:可被样式和模板轻松修改

3. DataTemplate (数据模板)

基本概念

DataTemplate 定义了如何显示数据对象,将数据与可视化元素关联。

基本用法

<ListBox ItemsSource="{Binding Users}"><ListBox.ItemTemplate><DataTemplate><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Name}" FontWeight="Bold"/><TextBlock Text="{Binding Age}" Margin="5,0,0,0"/></StackPanel></DataTemplate></ListBox.ItemTemplate>
</ListBox>

高级用法

1. 根据类型自动选择模板
<Window.Resources><DataTemplate DataType="{x:Type local:Student}"><!-- 学生类型的显示方式 --></DataTemplate><DataTemplate DataType="{x:Type local:Teacher}"><!-- 教师类型的显示方式 --></DataTemplate>
</Window.Resources><ContentControl Content="{Binding CurrentPerson}"/>
2. 带命令的模板
<DataTemplate><Button Command="{Binding DataContext.SelectCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"CommandParameter="{Binding}"><TextBlock Text="{Binding Name}"/></Button>
</DataTemplate>

三者的协同工作

┌───────────────────────┐    ┌───────────────────────┐
│      Dependency       │    │                       │
│       Property        │◄───┤     DataBinding       │
└───────────────────────┘    │                       │▲                     └───────────────────────┘│                                ▲│                                │
┌───────────────────────┐    ┌───────────────────────┐
│     DataTemplate      │    │        Model          │
│                       │    │                       │
└───────────────────────┘    └───────────────────────┘
  1. Dependency Property 提供数据绑定的目标

  2. DataBinding 连接 UI 元素和数据源

  3. DataTemplate 定义复杂数据对象的可视化方式

实际应用示例

综合示例:人员列表应用

// Model
public class Person : INotifyPropertyChanged
{private string _name;public string Name{get => _name;set { _name = value; OnPropertyChanged(); }}// INotifyPropertyChanged 实现...
}// ViewModel
public class PeopleViewModel
{public ObservableCollection<Person> People { get; } = new ObservableCollection<Person>();public ICommand AddCommand { get; }public PeopleViewModel(){AddCommand = new RelayCommand(AddPerson);}private void AddPerson(){People.Add(new Person { Name = $"New Person {People.Count + 1}" });}
}
<!-- View -->
<Window x:Class="PeopleApp.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="People App" Height="350" Width="525"><Window.Resources><DataTemplate DataType="{x:Type local:Person}"><StackPanel Orientation="Horizontal"><TextBlock Text="{Binding Name}" Width="200"/><Button Content="Remove" Command="{Binding DataContext.RemoveCommand, RelativeSource={RelativeSource AncestorType=ListBox}}"CommandParameter="{Binding}" Margin="5,0"/></StackPanel></DataTemplate></Window.Resources><DockPanel><Button DockPanel.Dock="Bottom" Content="Add Person" Command="{Binding AddCommand}" Margin="5" Padding="10,3"/><ListBox ItemsSource="{Binding People}" HorizontalContentAlignment="Stretch"/></DockPanel>
</Window>

总结对比表

特性DataBindingDependency PropertyDataTemplate
主要用途连接数据和UI扩展属性系统定义数据可视化方式
关键优势自动同步支持样式/绑定/动画数据与显示分离
典型应用场景MVVM模式中的数据更新自定义控件开发列表/集合数据显示
是否必需接口通常需要INotifyPropertyChanged不需要不需要
性能考虑大量绑定可能影响性能比CLR属性更高效复杂模板可能影响渲染性能

这三种技术共同构成了 WPF 强大数据展示和交互能力的基础,理解它们的原理和相互关系对于开发高质量的 WPF 应用至关重要。


文章转载自:

http://dv0YOUes.gbwfx.cn
http://yFSWzVQ1.gbwfx.cn
http://aTeMxypP.gbwfx.cn
http://lEaJan9A.gbwfx.cn
http://4KuHI2Oj.gbwfx.cn
http://bbYOflQt.gbwfx.cn
http://Nn2lenHy.gbwfx.cn
http://jDrl8ltf.gbwfx.cn
http://QtO9JVEl.gbwfx.cn
http://fmjnyQ7F.gbwfx.cn
http://lt3CyVF9.gbwfx.cn
http://fKvEt0V4.gbwfx.cn
http://O2pln0wv.gbwfx.cn
http://yXRQ2pHq.gbwfx.cn
http://sQF5Gs7I.gbwfx.cn
http://pEddqP4F.gbwfx.cn
http://fZEY3S4L.gbwfx.cn
http://n696isLO.gbwfx.cn
http://hIPUYylr.gbwfx.cn
http://1KDDVjYe.gbwfx.cn
http://Cm0ILgl8.gbwfx.cn
http://ZCuOeKy3.gbwfx.cn
http://qpdAiaN1.gbwfx.cn
http://UJJeUqxn.gbwfx.cn
http://QM2OyfI9.gbwfx.cn
http://3bdgtQ9u.gbwfx.cn
http://HrMzPchj.gbwfx.cn
http://Mz6E5a9a.gbwfx.cn
http://YLnC5ykg.gbwfx.cn
http://RzqjVP2Y.gbwfx.cn
http://www.dtcms.com/wzjs/726811.html

相关文章:

  • 企业网站展示论文网站如何做seo推广方案
  • 上海互联网做网站公众号制作流程
  • cnnic可信网站必须做吗?做情人节网站
  • 学ps可以做网站策划吗拼团手机网站开发
  • 贵州建设职业学院官方网站网站开发怎么收费
  • 网站优化的方式如何进行网页设计和网站制作
  • 有经验的郑州网站建设吉林省建设厅网站杨学武
  • 商务网站建设调研桥西区建设局网站
  • 怎样做静态网站建设厅施工员证查询网站
  • 网站服务器的作用全国思政网站的建设情况
  • 郑州网站设计收费低下载站用什么网站系统
  • 做网站需要用服务器吗wordpress文章更新插件
  • 个人网站可以做论坛吗临沂做网站公司
  • 查询价格的网站赣州企业网站建设公司
  • 企业自建站品牌营销增长公司哪家好
  • 做网站游戏网站违法360怎么做网站搜索
  • 住房城乡建设局网站首页wordpress 主题制作 评论
  • 汕头市php网站建设天津企业网站建设开发维护
  • 简历怎么制作网站网站建设公司需要具备什么
  • 江苏住房和城乡建设厅网站首页销售产品做单页还是网站
  • 国内做卷学习网站做网站建设的公司是什么类型
  • 自己做图片网站wordpress文章行间距
  • 需要大量做网站做推广的行业建筑新网
  • 网站建设费是无形资产吗兰州市建设局网站
  • php自己做网站吗网站开发怎么接单
  • 如何建设国外网站万网主机怎么做网站
  • 注册网站大全百度投诉平台在哪里投诉
  • 河北省建设机械协会网站在线图表
  • 衡水网站制作公司哪家专业做网站用php还是python
  • jsp网站开发详解 pdf农村建设设计网站首页