WPF中依赖属性及附加属性的概念及用法

编程入门 行业动态 更新时间:2024-10-11 19:15:41

WPF中依赖<a href=https://www.elefans.com/category/jswz/34/1771415.html style=属性及附加属性的概念及用法"/>

WPF中依赖属性及附加属性的概念及用法

完全来源于十月的寒流,感谢大佬讲解

依赖属性

由依赖属性提供的属性功能
与字段支持的属性不同,依赖属性扩展了属性的功能。 通常,添加的功能表示或支持以下功能之一:

  • 资源
  • 数据绑定
  • 样式
  • 动画
  • 元数据重写
  • 属性值继承
  • WPF 设计器集成
    public partial class MainWindow : Window{public MainWindow(){InitializeComponent();CustomTextBox customTextBox = new CustomTextBox();customTextBox.IsHightLighted = true;customTextBox.SetValue(CustomTextBox.IsHightLightedProperty, true);}}public class CustomTextBox : TextBox{public bool IsHightLighted{get { return (bool)GetValue(IsHightLightedProperty); }set { SetValue(IsHightLightedProperty, value); }}public static readonly DependencyProperty IsHightLightedProperty =DependencyProperty.Register("IsHightLighted", typeof(bool), typeof(CustomTextBox), new PropertyMetadata(false));}
#region HasText
public bool HasText => (bool)GetValue(HasTextProperty);public static readonly DependencyProperty HasTextProperty;
public static readonly DependencyPropertyKey HasTextPropertyKey;static CustomTextBox()
{HasTextPropertyKey = DependencyProperty.RegisterReadOnly("HasText",typeof(bool),typeof(CustomTextBox),new PropertyMetadata(false));HasTextProperty = HasTextPropertyKey.DependencyProperty;
}
#endregion
<Window x:Class="Test_05.MainWindow"xmlns=""xmlns:x=""xmlns:d=""xmlns:mc=""xmlns:local="clr-namespace:Test_05"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.Resources><Style TargetType="local:CustomTextBox"><Style.Triggers><Trigger Property="IsHightLighted" Value="True"><Setter Property="Background" Value="Yellow"></Setter></Trigger><Trigger Property="IsHightLighted" Value="False"><Setter Property="Background" Value="Blue"></Setter></Trigger></Style.Triggers></Style></Window.Resources><StackPanel><local:CustomTextBox Text="Hello World!" FontSize="30" IsHightLighted="True"></local:CustomTextBox></StackPanel>
</Window>

附加属性

    <StackPanel><!--<local:CustomTextBox Text="Hello World!" FontSize="30" IsHightLighted="True"></local:CustomTextBox>--><local:CustomTextBox Text="Hello World!" FontSize="30"><local:CustomTextBox.IsHightLighted>true</local:CustomTextBox.IsHightLighted><Grid.Row>1</Grid.Row></local:CustomTextBox></StackPanel>

更多推荐

WPF中依赖属性及附加属性的概念及用法

本文发布于:2023-11-16 17:59:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630194.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   概念   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!