营销网站建设哪里便宜,北京朝阳区,我做的网站怎么是危险网站,网站 做 app开发完全来源于十月的寒流#xff0c;感谢大佬讲解
依赖属性 由依赖属性提供的属性功能 与字段支持的属性不同#xff0c;依赖属性扩展了属性的功能。 通常#xff0c;添加的功能表示或支持以下功能之一#xff1a;
资源数据绑定样式动画元数据重写属性值继承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;
}
#endregionWindow x:ClassTest_05.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:Test_05mc:IgnorabledTitleMainWindow Height450 Width800Window.ResourcesStyle TargetTypelocal:CustomTextBoxStyle.TriggersTrigger PropertyIsHightLighted ValueTrueSetter PropertyBackground ValueYellow/Setter/TriggerTrigger PropertyIsHightLighted ValueFalseSetter PropertyBackground ValueBlue/Setter/Trigger/Style.Triggers/Style/Window.ResourcesStackPanellocal:CustomTextBox TextHello World! FontSize30 IsHightLightedTrue/local:CustomTextBox/StackPanel
/Window附加属性 StackPanel!--local:CustomTextBox TextHello World! FontSize30 IsHightLightedTrue/local:CustomTextBox--local:CustomTextBox TextHello World! FontSize30local:CustomTextBox.IsHightLightedtrue/local:CustomTextBox.IsHightLightedGrid.Row1/Grid.Row/local:CustomTextBox/StackPanelStackPanellocal:CustomTextBox x:Nameaoteman local:TextBoxHelper.Titlethis is test FontSize30Text{Binding RelativeSource{RelativeSource self}, Path(local:TextBoxHelper.Title)}/local:CustomTextBox!--local:CustomTextBox x:Nameaoteman FontSize30Text{Binding RelativeSource{RelativeSource self}, Path(local:TextBoxHelper.Title)}/local:CustomTextBox--
/StackPanelpublic partial class MainWindow : Window
{public MainWindow(){InitializeComponent();//TextBoxHelper.SetTitle(aoteman, this is aoteman);}
}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));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;}
}public class TextBoxHelper
{public static string GetTitle(DependencyObject obj){return (string)obj.GetValue(TitleProperty);}public static void SetTitle(DependencyObject obj, string value){obj.SetValue(TitleProperty, value);}// Using a DependencyProperty as the backing store for Title. This enables animation, styling, binding, etc...public static readonly DependencyProperty TitleProperty DependencyProperty.RegisterAttached(Title, typeof(string), typeof(TextBoxHelper), new PropertyMetadata());
}TextBox HasText实现一
Window x:ClassTest_01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:Test_01mc:IgnorabledTitleMainWindow Height450 Width800Window.Resources/Window.ResourcesStackPanel!--local:CustomTextBox x:Nameaoteman local:TextBoxHelper.Titlethis is test FontSize30Text{Binding RelativeSource{RelativeSource self}, Path(local:TextBoxHelper.Title)}/local:CustomTextBox--TextBox x:NametBox Text1234 FontSize30 local:TextBoxHelper.MonitorTextChangeTrue/TextBoxCheckBox IsChecked{Binding ElementNametBox, Path(local:TextBoxHelper.HasText)} FontSize30/CheckBox/StackPanel
/Windowusing System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace Test_01
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();}}public class TextBoxHelper{public static bool GetHasText(DependencyObject obj){return (bool)obj.GetValue(HasTextProperty);}public static void SetHasText(DependencyObject obj, bool value){obj.SetValue(HasTextProperty, value);}public static readonly DependencyProperty HasTextProperty DependencyProperty.RegisterAttached(HasText,typeof(bool),typeof(TextBoxHelper),new PropertyMetadata(false));public static bool GetMonitorTextChange(DependencyObject obj){return (bool)obj.GetValue(MonitorTextChangeProperty);}public static void SetMonitorTextChange(DependencyObject obj, bool value){obj.SetValue(MonitorTextChangeProperty, value);}// Using a DependencyProperty as the backing store for MonitorTextChange. This enables animation, styling, binding, etc...public static readonly DependencyProperty MonitorTextChangeProperty DependencyProperty.RegisterAttached(MonitorTextChange,typeof(bool),typeof(TextBoxHelper),new PropertyMetadata(false, MonitorTextChangedPropertyChanged));private static void MonitorTextChangedPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e){if (d is TextBox box false){throw new NotSupportedException();}if ((bool)e.NewValue){box.TextChanged TextChanged;SetHasText(box, !string.IsNullOrEmpty(box.Text));}else{box.TextChanged - TextChanged;}}private static void TextChanged(object sender, TextChangedEventArgs e){var box sender as TextBox;SetHasText(box, !string.IsNullOrEmpty(box.Text));}}
}TextBox HasText只读实现二
Window x:ClassTest_01.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:Test_01mc:Ignorabled TitleMainWindow Height450 Width800Window.Resources/Window.ResourcesStackPanel!--local:CustomTextBox x:Nameaoteman local:TextBoxHelper.Titlethis is test FontSize30Text{Binding RelativeSource{RelativeSource self}, Path(local:TextBoxHelper.Title)}/local:CustomTextBox--TextBox x:NametBox Text1234 FontSize30 local:TextBoxHelper.MonitorTextChangeTrue/TextBoxCheckBox IsChecked{Binding ElementNametBox, Path(local:TextBoxHelper.HasText), ModeOneWay} FontSize30/CheckBox/StackPanel
/Window
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;namespace Test_01
{/// summary/// MainWindow.xaml 的交互逻辑/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();}}public class TextBoxHelper{static TextBoxHelper(){HasTextProperty HasTextPropertyKey.DependencyProperty;}public static bool GetHasText(DependencyObject obj){return (bool)obj.GetValue(HasTextProperty);}public static void SetHasText(DependencyObject obj, bool value){obj.SetValue(HasTextPropertyKey, value);}public static readonly DependencyProperty HasTextProperty;public static readonly DependencyPropertyKey HasTextPropertyKey DependencyProperty.RegisterAttachedReadOnly(HasText,typeof(bool),typeof(TextBoxHelper),new PropertyMetadata(false));public static bool GetMonitorTextChange(DependencyObject obj){return (bool)obj.GetValue(MonitorTextChangeProperty);}public static void SetMonitorTextChange(DependencyObject obj, bool value){obj.SetValue(MonitorTextChangeProperty, value);}public static readonly DependencyProperty MonitorTextChangeProperty DependencyProperty.RegisterAttached(MonitorTextChange,typeof(bool),typeof(TextBoxHelper),new PropertyMetadata(false, MonitorTextChangedPropertyChanged));private static void MonitorTextChangedPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e){if (d is TextBox box false){throw new NotSupportedException();}if ((bool)e.NewValue){box.TextChanged TextChanged;SetHasText(box, !string.IsNullOrEmpty(box.Text));}else{box.TextChanged - TextChanged;}}private static void TextChanged(object sender, TextChangedEventArgs e){var box sender as TextBox;SetHasText(box, !string.IsNullOrEmpty(box.Text));}}
}{Binding}解释
local:TextBoxHelper.MonitorTextChangeTrue这是自定义属性local:TextBoxHelper.MonitorTextChange的设置是自定义控件或附加属性的一部分。这个属性被用来监测文本的变化。CheckBox IsChecked{Binding ElementNametBox, Path(local:TextBoxHelper.HasText)} FontSize30这是一个复选框控件其中包含了数据绑定。 IsChecked{Binding ...}这部分指示IsChecked属性将被绑定到某个数据源。ElementNametBox这是一个Binding的设置指定了数据源是哪个控件即名为tBox的TextBox控件。Path(local:TextBoxHelper.HasText)这是Binding的路径设置。告诉WPF应用程序查找名为tBox的控件并在该控件上查找名为HasText的属性。这个属性用于确定TextBox中是否有文本。
关于这些概念的详细解释
ElementName这是一个Binding设置用于指定绑定的数据源控件的名称。在示例中数据源是名为tBox的TextBox。Path这是用于指定数据源控件上的属性或属性路径的设置。在这里你正在查找名为HasText的属性该属性被用于确定CheckBox的IsChecked属性。(local:TextBoxHelper.HasText)这种写法通常用于引用附加属性其中local表示当前命名空间TextBoxHelper是一个附加属性的名称而HasText是这个附加属性的属性名。 这种绑定方式可以用于将一个控件的属性绑定到另一个控件的属性使它们保持同步例如在文本框中输入文本时相关的复选框的选中状态也会相应地改变。这种绑定通常用于实现界面元素之间的交互和数据同步。
e.NewValue解释 e.NewValue 是一个 DependencyPropertyChangedEventArgs 对象的属性它用于存储关于依赖属性更改的信息。
含义e.NewValue 表示在依赖属性更改时属性的新值。当一个依赖属性的值发生变化时e.NewValue 将包含新值以便可以在事件处理程序中访问并使用它。作用e.NewValue 主要用于在属性更改事件处理程序中获取属性的新值。它允许在属性值发生变化时采取相应的操作。例如可以根据新值来更新界面元素、执行计算或触发其他操作。类型e.NewValue 的类型取决于被监测属性的类型。它是一个 object 类型因为它需要能够表示任何类型的值。通常需要将其转换为适当的类型以便在代码中使用。这个类型转换通常是基于知道将要更改的属性的类型。例如在上述代码示例中似乎假定属性的新值是一个布尔值。
在提供的代码示例中if ((bool)e.NewValue) 的作用是检查某个依赖属性的新值是否为 true以决定是否执行特定的操作。这是一个典型的用例其中 e.NewValue 用于确定如何响应属性的更改。