How to create a Dependency property on an existing control?

前端 未结 3 1749
清歌不尽
清歌不尽 2021-01-12 05:20

I have been reading on Dependency properties for a few days and understand how they retrieve the value rather than to set/get them as in CLR properties. Feel free to correct

相关标签:
3条回答
  • 2021-01-12 05:48

    You cant add DependencyProperties to existing type. While you can use AttachedProperty, logic behind using it and deriving new type is completly different.

    In your case I would recomend to derive new type. Mainly because your logic is bound with this type. This is basic behind inheritance and is not bound with Dependency properties.

    In case of AttachedProperty you are only giving another object awerness of values in different object. Something like Grid.Row is giving Grid awerness of its child and how it should position it. Object where this property is set is not aware of anything.

    0 讨论(0)
  • 2021-01-12 05:51

    Here is an example on an ovveride 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";
            }
        }
    }
    

    An the corresponding 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 讨论(0)
  • 2021-01-12 05:52

    You can use attached properties for it.

    Define your property MyInt:

    
    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);
            }
        }
    }
    
    

    Bind label 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>
    
    0 讨论(0)
提交回复
热议问题