WPF--DependencyProperty

本文详细介绍了WPF中依赖属性的定义与使用方法,包括如何创建依赖属性、如何通过依赖属性实现属性值的变化监听,以及如何结合依赖属性进行编辑模式的控制等。文章通过具体的代码示例展示了依赖属性在实际应用中的作用。

 WPF中的依赖属性的写法:

  【依赖属性为扩展类的使用】

  参考http://msdn.microsoft.com/zh-cn/library/cc221408(v=VS.95).aspx

  内容。

public bool IsEditable
{
get { return (bool)GetValue(IsEditableProperty); }
set { SetValue(IsEditableProperty, value); }
}
public static readonly DependencyProperty IsEditableProperty =
DependencyProperty.Register(
"IsEditable",
typeof(bool),
typeof(User_Node),
new PropertyMetadata(true));

public bool IsInEditMode
{
get
{
if (IsEditable)
return (bool)GetValue(IsInEditModeProperty);
else
return false;
}
set
{
if (IsEditable)
{
if (value) oldText = Text;
SetValue(IsInEditModeProperty, value);
}
}
}
public static readonly DependencyProperty IsInEditModeProperty =
DependencyProperty.Register(
"IsInEditMode",
typeof(bool),
typeof(User_Node),
new PropertyMetadata(false));

public string TextFormat
{
get { return (string)GetValue(TextFormatProperty); }
set
{
if (value == "") value = "{0}";
SetValue(TextFormatProperty, value);
}
}
public static readonly DependencyProperty TextFormatProperty =
DependencyProperty.Register(
"TextFormat",
typeof(string),
typeof(User_Node),
new PropertyMetadata("{0}"));


---------------------------在修改时响应事件的写法-------------------------------

public string Text
{
get { return (string)GetValue(TextProperty); }
set { SetValue(TextProperty, value); }
}
public static readonly DependencyProperty TextProperty =
DependencyProperty.Register(
"Text",
typeof(string),
typeof(User_Node),
new PropertyMetadata("", OnChanged));
private static void OnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
string a = "";
}

本文来自来打工的的博客,原文地址:http://blog.sina.com.cn/s/blog_3f0612650100r0bs.html

04-27
### WPF-UI Framework Usage Examples and Features #### Data Binding in WPF WPF provides robust data binding capabilities, allowing developers to bind UI elements directly to data sources. For instance, the `DataGrid` control supports advanced editing scenarios through its integration with the `IEditableObject` interface[^2]. When implementing custom business objects, one must explicitly define how these objects handle transactions by implementing this interface. Here’s an example demonstrating basic data binding using a simple ViewModel: ```csharp public class Person : INotifyPropertyChanged, IEditableObject { private string _name; public string Name { get => _name; set { if (_name != value) { _name = value; OnPropertyChanged(nameof(Name)); } } } public void BeginEdit() { // Save current state before edits begin. } public void CancelEdit() { // Restore original values upon cancellation. } public void EndEdit() { // Commit changes after successful edit completion. } public event PropertyChangedEventHandler PropertyChanged; protected virtual void OnPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } } ``` This code snippet illustrates how to create a model that adheres to both `INotifyPropertyChanged` for property change notifications and `IEditableObject` for transactional updates within grids like `DataGrid`. #### Event Aggregation Pattern in WPF Applications For decoupled communication between components in large-scale applications built on top of WPF, leveraging patterns such as Event Broker from Composite UI Application Block (CAB) or Event Aggregator from Prism becomes essential[^1]. These mechanisms allow loosely coupled parts of an application to communicate without direct references to each other. Below is an illustrative implementation utilizing Prism's `EventAggregator` service: ```csharp using Prism.Events; // Define a Pub/Sub message type public class MessagePublishedEvent : PubSubEvent<string> { } // Publisher side logic var ea = Container.Resolve<IEventAggregator>(); ea.GetEvent<MessagePublishedEvent>().Publish("Message sent!"); // Subscriber side logic ea.GetEvent<MessagePublishedEvent>().Subscribe(message => { Console.WriteLine($"Received: {message}"); }); ``` In this case, publishers send messages while subscribers listen for them asynchronously, promoting separation of concerns across modules. #### Custom Controls Development Developers often extend default controls provided out-of-the-box by creating derived classes tailored specifically towards their needs. Below shows extending Button behavior adding extra properties useful during runtime configuration dynamically. ```xml <Window x:Class="CustomControlExample.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanel> <!-- Example usage --> <local:ExtendedButton Content="Click Me!" IsEnabledDynamicBinding="{Binding Path=SomeCondition}" /> </StackPanel> </Window> <!-- Code Behind Definition --> public class ExtendedButton : Button { public static readonly DependencyProperty IsEnabledDynamicBindingProperty = DependencyProperty.RegisterAttached( "IsEnabledDynamicBinding", typeof(bool), typeof(ExtendedButton)); public bool IsEnabledDynamicBinding { get { return (bool)this.GetValue(IsEnabledDynamicBindingProperty); } set { this.SetValue(IsEnabledDynamicBindingProperty, value); } } } ``` Such extensions provide flexibility enabling richer interactions based entirely around declaratively defined attributes inside XAML markup files themselves rather than procedural coding approaches elsewhere outside views altogether.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值