How to create a Dependency property for an existing control?

I read Dependency properties for several days and understand how they retrieve the value, rather than set / get them as in the CLR properties. Feel free to correct me if I am wrong.

From my understanding, all WPF controls, such as TextBlock, Button, etc. that are derived from DependencyObject will also contain dependency properties to store their values ​​instead of using CLR properties. This has the advantage of overriding local values ​​when using animations or inheriting values, if the local value is not set at all, etc.

Now I'm trying to come up with some examples for creating and using my own dp.

1) Can I create my own dependency property for an existing WPF control? Suppose I need a dependency property of type integer in a WPF Textblock class? Or do I need to create a new class derived from TextBlockBase to create my dependency property above?

2) In any case, let's say I created a dependency property in a WPF text block class. Now I would like to use it, linking the contents of the label with this TextBlock dependency property. So that the label always displays the actual TextBlock dp value, regardless of whether it is inherited or set locally.

Hope someone can help me with these two examples ... Thanks a lot, Kava

+7
wpf dependency-properties
source share
3 answers

You can use the attached properties for it.

Define your MyInt property:

namespace WpfApplication5 { public class MyProperties { public static readonly System.Windows.DependencyProperty MyIntProperty; static MyProperties() { MyIntProperty = System.Windows.DependencyProperty.RegisterAttached( "MyInt", typeof(int), typeof(MyProperties)); } public static void SetMyInt(System.Windows.UIElement element, int value) { element.SetValue(MyIntProperty, value); } public static int GetMyInt(System.Windows.UIElement element) { return (int)element.GetValue(MyIntProperty); } } } 

Link shortcut content:

 <Window x:Class="WpfApplication5.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication5" Title="Window1" Height="300" Width="300"> <Grid> <Label Margin="98,115,51,119" Content="{Binding Path=(local:MyProperties.MyInt), RelativeSource={x:Static RelativeSource.Self}}" local:MyProperties.MyInt="42"/> </Grid> </Window> 
+6
source share

You cannot add DependencyProperties to an existing type. Although you can use AttachedProperty, the logic for using and getting a new type is completely different.

In your case, I would recommend getting a new type. Mainly because your logic is related to this type. This is the main behind inheritance and is not related to Dependency properties.

In the case of AttachedProperty, you simply give the value of values ​​in another object to another object. Something like Grid.Row gives the grid trait of his child and how he should position it. The object in which this property is set does not know anything.

+1
source share

Here is an example on the override of the Run element:

 using System; using System.Windows; using System.Windows.Documents; namespace MyNameSpace.FlowDocumentBuilder { public class ModifiedRun : Run { static DateRun() { TextProperty.OverrideMetadata(typeof(DateRun),new FrameworkPropertyMetadata(string.Empty,FrameworkPropertyMetadataOptions.Inherits,null,CoerceValue)); } private static object CoerceValue(DependencyObject d, object basevalue) { return "AAAAAAAA"; } } } 

The relevant XAML is:

 <FlowDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ColumnWidth="400" FontSize="14" FontFamily="Arial" xmlns:local="clr-namespace:MyNameSpace.FlowDocumentBuilder;assembly=MyNameSpace.FlowDocumentBuilder" > <Paragraph><local:ModifiedRun Text="BBBBBBBBBB"/></Paragraph> </FlowDocument> 
0
source share

All Articles