- 浏览: 399676 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
You might wonder what does the DependencyProperty.AddOwner do. in Zeeshan Amjad's blog - AddOwner functionality of Dependency Property in WPF he gaves some example.
Ialso looked up the MSDN documentation - DependencyProperty.AddOwner Method (Type, PropertyMetadata);
The return value of this method is used to declare and expose the dependency property, particularly as it exists on the adding owner class. Generally, the same property name for both original owner and added owner should be used to indicate the similar functionality. It is good practice to expose the identifiers, as well as new CLR property wrappers, for dependency properties that are added to types using AddOwner.
The AddOwner methodology recommended above is used when creating APIs declared within WPF. For instance, both Border and Controldefine a BorderBrush dependency property, which have similar functionality. Control defines its BorderBrush property to the property system by calling AddOwner on original owner Border and its registered BorderBrushProperty dependency property identifer. The AddOwnerreturn value is then used to establish a static DependencyProperty field (BorderBrushProperty)for that property on the added owner, and aBorderBrush property wrapper is also declared.
I wrote a test program.
namespace AddOwnerTest { public class BaseControl : Control { public string MyDp { get { return (string)GetValue(MyDpProperty); } set { SetValue(MyDpProperty, value); } } // Using a DependencyProperty as the backing store for MyDp. This enables animation, styling, binding, etc... public static readonly DependencyProperty MyDpProperty = DependencyProperty.Register("MyDp", typeof(string), typeof(BaseControl), new UIPropertyMetadata(string.Empty, new PropertyChangedCallback(BaseControlMyDpPropertyChangeCallback))); public static void BaseControlMyDpPropertyChangeCallback(DependencyObject dp, DependencyPropertyChangedEventArgs e) { // Console.WriteLine("base Dependency Property change d"); } } }
and Iwrote two Views, UserControl1, and UserControl2.
UserControl1.xaml.cs
namespace AddOwnerTest.Views { /// <summary> /// Interaction logic for UserControl1.xaml /// </summary> public partial class UserControl1 : UserControl { public UserControl1() { InitializeComponent(); MyDp = "1"; } public static readonly DependencyProperty MyDpProperty; public string MyDp { get { return (string)GetValue(MyDpProperty); } set { SetValue(MyDpProperty, value); } } static UserControl1() { UserControl1.MyDpProperty = BaseControl.MyDpProperty.AddOwner(typeof(UserControl1), new PropertyMetadata(string.Empty, new PropertyChangedCallback(MyDpUserControl1ChangedCalback))); } public static void MyDpUserControl1ChangedCalback(DependencyObject dp, DependencyPropertyChangedEventArgs e) { // Console.WriteLine("MyDpProperty changed in UserControl1"); } } }
and the UserControl1.xaml
<UserControl x:Class="AddOwnerTest.Views.UserControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" > <WrapPanel > <TextBlock Text="User Control 1:" /> <TextBox Text="{Binding Path=MyDp, Mode=TwoWay,RelativeSource={RelativeSource Mode=FindAncestor, AncestorLevel=2,AncestorType=UserControl}}" /> </WrapPanel> </UserControl>
Similarly, the code of UserControl2.xaml.cs
namespace AddOwnerTest.Views { /// <summary> /// Interaction logic for UserControl2.xaml /// </summary> public partial class UserControl2 : UserControl { public UserControl2() { InitializeComponent(); } public static readonly DependencyProperty MyDpProperty; public string MyDp { get { return (string)GetValue(MyDpProperty); } set { SetValue(MyDpProperty, value); } } static UserControl2() { UserControl2.MyDpProperty = BaseControl.MyDpProperty.AddOwner(typeof(UserControl2), new PropertyMetadata(string.Empty, new PropertyChangedCallback(MyDpUserControl2ChangedCalback))); } public static void MyDpUserControl2ChangedCalback(DependencyObject dp, DependencyPropertyChangedEventArgs e) { // Console.WriteLine("MyDpProperty changed in UserControl2"); } } }
and the UserControl2.xaml
namespace AddOwnerTest.Views { /// <summary> /// Interaction logic for UserControl2.xaml /// </summary> public partial class UserControl2 : UserControl { public UserControl2() { InitializeComponent(); } public static readonly DependencyProperty MyDpProperty; public string MyDp { get { return (string)GetValue(MyDpProperty); } set { SetValue(MyDpProperty, value); } } static UserControl2() { UserControl2.MyDpProperty = BaseControl.MyDpProperty.AddOwner(typeof(UserControl2), new PropertyMetadata(string.Empty, new PropertyChangedCallback(MyDpUserControl2ChangedCalback))); } public static void MyDpUserControl2ChangedCalback(DependencyObject dp, DependencyPropertyChangedEventArgs e) { // Console.WriteLine("MyDpProperty changed in UserControl2"); } } }
Last, is the MainWindow.xaml file
<Window x:Class="AddOwnerTest.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" xmlns:local="clr-namespace:AddOwnerTest" xmlns:views="clr-namespace:AddOwnerTest.Views" > <Window.Resources> <local:BaseControl x:Key="BaseControl"/> </Window.Resources> <StackPanel> <views:UserControl1 /> <views:UserControl2 /> <!--<TextBox Text="{Binding Path=Parent.Children[0].MyDp, Mode=TwoWay, RelativeSource={RelativeSource Self}}" />--> <TextBox Text="{Binding Path=MyDp, Mode=TwoWay, Source={StaticResource BaseControl}}" /> </StackPanel> </Window>
You won't see result is quite contrary to the hypothesis.
// this is to test the add owner call from the DependnecyProperty class. // ideally, if you add an owner to a existing DependencyProperty, then // the newly added owner should be notified if the underlying data updates. // // the test is to create two views, and they both are added as an owner to an // DP that is declared in a base class // Then we try to update the base class' DP (by updating the model behind), and we are going to see if // two views's displa is updated. //
This is because addOwner create a new metadata under the DependencyProperty for the adding class. And it does not create some new DependecyProperty or what so ever.
So, in a nutshell, the AddOwner register a new MetaData for the adding classes. It is used to decrease the duplication on register a new DependencyProperty.
Also, with sharing the same DependencyProperty, you can pass different DependencyObject to get the Metadata tha is assiciated with the Type/DO..
See more on the Dependency Property Metadata
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1976first let’s see an example ... -
WPF – Virtualization – VirutalizationStackPanel and ItemsPanelTemplate
2013-08-05 21:55 1410Topic: WPF – Virtualization – ... -
wpf - BehaviorBase and one use examples
2013-06-18 18:41 1311Behavior is something that we ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1918You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5330As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1185Recently I was trying to debug ... -
wpf - default implicit style
2013-05-10 10:24 794We know that if you left out ... -
wpf - Style setter on the attached property
2013-05-08 14:54 2851I believe that you are familia ... -
wpf - specify enum values in xaml
2013-05-08 11:31 3585Many a situation you find tha ... -
wpf - IG xamDataGrid bind to XmlDataProvider with Xml Island
2012-12-18 14:28 1286Sometimes you may bind to some ... -
wpf - translate winform button/mouse event to wpf events
2012-12-12 17:37 2162It is common that we sometimes ... -
wpf - Freezable and its meaning
2012-09-27 12:38 0This article is based on the di ... -
wpf - Customize the grid lines for original wpf Grid control
2012-09-27 12:01 1458The System.WIndows.Controls.Gri ... -
c# - Convert from System.Drawing.Image to System.WIndows.Media.ImageSource
2012-09-25 14:27 7417In Previous discussion, we have ... -
wpf - Get Effective DependencyProperty value on a DependencyObject
2012-08-28 19:05 1044As discussed in my previous pos ... -
wpf - Default(Theme) style and its DefaultStyleKey
2012-08-28 17:54 1387As dicsused in the subsection o ... -
wpf - Dependency Property Value Precedence
2012-08-28 18:56 882A dependency property to an Dep ... -
wpf - WPF TemplateBinding vs RelativeSource TemplatedParent
2012-08-28 14:20 3713This is a post that summarizes ... -
wpf - ICutomTypeDescriptor , PropertyDescriptor and its use in PropertyGrid
2012-08-28 14:04 3580The type ICustomTypeDe ... -
wpf - tips to convert UI controls in WPF/Silverlight/Winforms into a Bitmap
2012-08-27 17:44 975In previous discussion, we have ...
相关推荐
通用WPF主题控件包rubyer-wpf-master是一款专为Windows Presentation Foundation (WPF) 应用程序设计的开源UI框架。它提供了丰富的主题和控件,旨在帮助开发者快速构建美观且用户友好的应用程序界面。在2.0.0版本中...
gong-wpf-dragdrop, GongSolutions.WPF.DragDrop 库是WPF的拖动'n'拖放框架 简介GongSolutions.WPF.DragDrop 库是一个易于使用的拖拉'n'拖放框架。特性使用 MVVM: 拖放逻辑可以放在ViewModel中。 代码不需要放在in中...
标题中的“wpf-mvvm-DeskTop-Sample-master”表明这是一个关于WPF(Windows Presentation Foundation)桌面应用程序的示例项目,使用了MVVM(Model-View-ViewModel)设计模式。这个项目是用C#编程语言编写的,面向的...
**WPF-Blockly** 是一个基于Windows Presentation Foundation (WPF) 的图形化编程工具,它为用户提供了构建和设计程序的直观界面。WPF作为Microsoft .NET Framework的一部分,主要用于构建桌面应用程序,它提供了...
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手
在这个名为"WPF-ControlBase-master.zip"的压缩包中,我们可以推测它包含了一个基于WPF的控制库项目,可能是一个开源或者个人开发的项目,用于提供自定义的WPF控件。这些控件可能是对标准WPF控件的扩展或增强,也...
WPF的基本空间历程,使用.net core3.0.1版本
【标题】"基于WPF的图形化编程控件和环境WPF-Blockly-master" 提供了一个创新的编程体验,它将传统的代码编写转变为图形化的流程图形式,使得编程变得更加直观和易于理解。WPF(Windows Presentation Foundation)是...
Bootstrap-WPF 样式是一种将流行的前端框架 Bootstrap 的设计风格应用于 WPF(Windows Presentation Foundation)应用程序的方法。Bootstrap 是一个广泛使用的开源工具包,主要用于构建响应式、移动设备优先的网页...
通过深入研究WPF-Diagram-Designer的源代码(如WPF-Diagram-Designer-master文件夹中的内容),开发者不仅可以学习到如何在WPF中构建复杂的图形界面,还可以了解到图形编辑器的设计原理和实现细节,对于提升图形应用...
在本文中,我们将深入探讨"AI-wpf-controls",这是一个专为Windows Presentation Foundation(WPF)框架设计的控件库。这个独特的库整合了多个知名控件库的优点,包括MahApps.Metro、Material-Design、HandyControl...
在本项目"WPF-MaterialDesign-master.zip"中,重点在于利用**Material Design**这一设计语言来增强WPF应用的视觉效果。Material Design是Google推出的一种设计规范,其灵感来源于现实世界中的纸张和墨水,强调层次感...
在“Wpf-glTF-testing.zip”压缩包中,我们有一个基于WPF(Windows Presentation Foundation)的简化glTF文件查看器项目。 WPF是.NET Framework的一部分,是一个用于构建Windows桌面应用程序的框架。它提供了丰富的...
“wpf---StatusBar”这个标题表明我们将探讨的是WPF(Windows Presentation Foundation)框架中的StatusBar组件。WPF是.NET Framework的一部分,用于构建桌面应用程序,它提供了丰富的用户界面(UI)功能。StatusBar...
**WPF - 强大的图表技术** Windows Presentation Foundation (WPF) 是Microsoft开发的一个用于构建桌面应用程序的框架,它提供了丰富的图形系统,包括对2D和3D图形的强大支持。在WPF中,开发人员可以利用各种图表...
【标题】"Wpf的Diagram画板aistudio-wpf-diagram-master" 是一个基于WPF(Windows Presentation Foundation)技术的图形设计工具,用于创建和编辑图表或流程图。这个项目是在原有的WPF-Diagram-Designer基础上进行的...
《OpenControls.Wpf-master:深度探索WPF框架控件》 在Windows Presentation Foundation(WPF)的世界里,开发者们能够创建出美观且功能丰富的桌面应用程序。OpenControls.Wpf-master项目,正如其名,是一个专注于...
在这个“wpf-datagrid-access DB”主题中,我们将深入探讨如何利用WPF Datagrid与Microsoft Access数据库进行交互,实现数据的读取、更新和保存。 1. **WPF Datagrid简介** - Datagrid是WPF中的一个数据展示控件,...
该资源"practical-wpf-charts-graphics-master.rar"包含了这本书的源代码,为读者提供了丰富的实践案例和深入理解WPF图表及图形编程的宝贵材料。 WPF(Windows Presentation Foundation)是.NET Framework的一部分...
C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手 C#开发WPF-Silverlight动画及游戏系列教程-深蓝色右手