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

建设官方网站怎么修改预留手机/seo狂人

建设官方网站怎么修改预留手机,seo狂人,广州个性化网站开发,什么叫网络推广DependencyObject和DependencyPorperty两个类是WPF属性系统的核心。 在WPF中,依赖对象的概念被DependencyObject类实现;依赖属性的概念则由DependencyPorperty类实现。 必须使用依赖对象作为依赖属性的宿主,二者结合起来,才能实现…

DependencyObject和DependencyPorperty两个类是WPF属性系统的核心。

在WPF中,依赖对象的概念被DependencyObject类实现;依赖属性的概念则由DependencyPorperty类实现。

必须使用依赖对象作为依赖属性的宿主,二者结合起来,才能实现完整的Binding目标被数据所驱动。DependencyObject具有GetValue和SetValue两个方法,用来获取/设置依赖属性的值。

DependencyObject是WPF系统中相当底层的一个基类,如下:

从这颗继承树可以看出,WPF的所有UI控件都是依赖对象。WPF的类库在设计时充分利用了依赖属性的优势,UI空间的饿绝大多数属性都已经依赖化了。

下面用一个简单的实例来说明依赖属性的使用方法。先准备好一个界面,顺便复习下前面的Style和Template:

<Window x:Class="DependencyObjectProperty.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"Title="MainWindow" Height="350" Width="525"><Window.Resources><Style x:Key="textStyle" TargetType="{x:Type TextBox}"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type TextBox}"><TextBlock Background="CadetBlue" Foreground="HotPink"  Text="{TemplateBinding Property=Text}"/></ControlTemplate></Setter.Value></Setter><Style.Triggers><Trigger Property="IsMouseOver" Value="true"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="{x:Type TextBox}"><Border SnapsToDevicePixels="true" x:Name="Bd" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}"><ScrollViewer SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" x:Name="PART_ContentHost" Background="AliceBlue"/></Border></ControlTemplate></Setter.Value></Setter></Trigger></Style.Triggers></Style></Window.Resources><StackPanel><TextBox Style="{StaticResource textStyle}" Height="37" Name="textBox1" FontSize="26"    Margin="5" Width="439" /><TextBox Style="{StaticResource textStyle}" Height="37" Name="textBox2" FontSize="26"  Margin="5" Width="439" /><Button Content="Button" Height="39" Name="button1" Width="131" Click="button1_Click" /></StackPanel>
</Window>

前面说过,DependencyProperty必须以DependencyObject为宿主、借助它的SetValue和GetValue方法进行写入和读取。因此,想用自定义的DependencyProperty,宿主一定是DependencyObject的派生类。

DependencyProperty实例的声明特点很明显:变量由public static readonly三个修饰符修饰,实例使用DependencyProperty.Register方法生成。而非new操作符得到。

代码如下:

using System.Windows;namespace DependencyObjectProperty
{class Student:DependencyObject{//定义依赖属性public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student));}
}

这是自定义DependencyProperty的最简单代码。
依赖属性也是属性,下面来使用它:

private void button1_Click(object sender, RoutedEventArgs e)
{Student stu = new Student();stu.SetValue(Student.NameProperty, textBox1.Text);textBox2.Text = (string)stu.GetValue(Student.NameProperty);
}

在textBox1中输入值,点下Button1后效果如下:

 

上面我们使用的依赖属性是靠GetValue和SetValue进行对外的暴露,而且在GetValue的时候需要进行类型的转换,因此,在大多数的情况下我们会为依赖属性添加一个CLR属性的外包装:

using System.Windows;namespace DependencyObjectProperty
{class Student:DependencyObject{//CLR属性进行封装public string Name{get { return (string)GetValue(NameProperty); }set { SetValue(NameProperty, value); }}//定义依赖属性public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student));}
}

有了这个CLR属性包装,我们就可以和CLR属性一样访问依赖属性了:

private void button1_Click(object sender, RoutedEventArgs e)
{Student stu = new Student();stu.Name = textBox1.Text;textBox2.Text = stu.Name;            
}

如果不关心底层的实现,下游的程序员在使用依赖属性时与使用单纯的CLR属性别无二致。

效果和上面相同:

 

 当然如果不用Binding,依赖属性的设计就没有意义,下面我们使用Binding把Student对象关联到textBox1上,再把textBox2关联到Student对象上。代码如下: 

private void button1_Click(object sender, RoutedEventArgs e)
{Student stu = new Student();
Binding binding = new Binding("Text") { Source = textBox1 };BindingOperations.SetBinding(stu, Student.NameProperty, binding);Binding binding2 = new Binding("Name") { Source = stu };BindingOperations.SetBinding(textBox2, TextBox.TextProperty, binding2); }

当然第二个Binding也可以这样写,下面两者等效:

Binding binding2 = new Binding("Name") { Source = stu };
BindingOperations.SetBinding(textBox2, TextBox.TextProperty, binding2);
textBox2.SetBinding(TextBox.TextProperty, binding2);

也可以在Student类中封装FrameworkElement类的SetBinding方法,如下:

using System.Windows;
using System.Windows.Data;namespace DependencyObjectProperty
{class Student:DependencyObject{//CLR属性进行封装public string Name{get { return (string)GetValue(NameProperty); }set { SetValue(NameProperty, value); }}//定义依赖属性public static readonly DependencyProperty NameProperty = DependencyProperty.Register("Name", typeof(string), typeof(Student));//SetBinding包装public BindingExpressionBase SetBinding(DependencyProperty dp, BindingBase binding){return BindingOperations.SetBinding(this, dp, binding);}}
}

则Binding可进一步写成这样:

private void button1_Click(object sender, RoutedEventArgs e)
{Student stu = new Student();stu.SetBinding(Student.NameProperty, new Binding("Text") { Source=textBox1 });textBox2.SetBinding(TextBox.TextProperty, new Binding("Name") { Source=stu});
}

效果如下:

//---------------------------------------------------------

自定义依赖属性也可以不需要手动进行声明、注册并使用CLR属性进行封装,只需要输入propdp,同时连按两次Tab,一个标准的依赖属性(带CLR属性包装)就声明好了。

prop:CLR属性

propa:附加属性

propdp:依赖属性

附加属性也是一种特别的依赖属性,顾名思义,附加属性是说一个属性本来不属于某个对象,但是由于某种需求而被后来附加上。也就是把对象放入一个特定的环境后对象才具有的属性,比如Canvas.Left DockPanel.Dock Grid.Column等。

声明时一样用public static readonly三个关键词修饰。唯一不同就是注册附加属性使用的是名为RegisterAttached的方法,但参数与Register方法相同。附加属性的包装器也与依赖属性不同,依赖属性使用CLR属性对GetValue和SetValue两个方法进行包装,附加属性则使用两个方法分别进行包装。

其可由propa+tab+tab方便的生成。理解附加属性的意义及使用场合即可。

转载于:https://www.cnblogs.com/DebugLZQ/archive/2012/11/30/2796021.html

http://www.lbrq.cn/news/1053883.html

相关文章:

  • 协会网站建设的优势/seo优化排名
  • 网站开发用户名不存在/宁波网站推广排名
  • 做网站的旅行社/企业网站建站模板
  • 河南做网站 河南网站建设/seo最好的工具
  • 湖南专业做网站公司有哪些/网站营销网站营销推广
  • 国内网站放国外服务器/网络广告推广平台
  • 深圳做网站建设/天津站内关键词优化
  • 西部数码做跳转网站/百度搜索页面
  • 如何做介绍监控公司的网站/信息流广告优化师
  • 莱芜新闻综合频道/seo关键字优化价格
  • 正规网站建设哪家好/软文平台
  • 建立企业网站的步骤/外链价格
  • 开平建设局网站/员工培训内容
  • 网站设计所用到的技术/淘宝运营培训机构
  • 做二手车网站需要什么手续/武汉今日新闻头条
  • 青岛网站建设案例/深圳搜索引擎优化推广
  • 做任务挣钱网站/怎么在网上做广告宣传
  • 做网站素材/濮阳网站推广
  • 深圳网站建设哪家公司好/关键词优化排名怎么做
  • 海盐网站建设/搜索引擎seo推广
  • 做网站哪家好 要钱/软文自助发稿平台
  • 广东省住房和城乡建设厅网站首页/推广竞价托管公司
  • 浙江专业做网站/什么是电商
  • 做网站要多少像素/产品互联网推广
  • 国外有哪些做服装的网站有哪些方面/百度热词指数
  • 做二维码推送网站/国内前10电商代运营公司
  • 厦门手机网站建设公司/网站排名优化软件
  • 找人代做网站费用/百度商业平台
  • 北京企业建网站定制价格/全网搜索软件
  • 宣城做w网站的公司/企业管理培训机构
  • 3D TOF 视觉相机:工业视觉的破局者,重塑视觉感知的未来
  • 电脑IP地址是“169.254.x.x”而无法上网的原因
  • 《C语言》函数练习题--3
  • Netty知识储备:BIO、NIO、Reactor模型
  • 将Excel数据导入SQL Server数据库,并更新源表数据
  • 超全的软件测试项目平台,10多个项目部署在线上环境,浏览器直接访问