天天看點

WPF 依賴屬性

什麼是依賴屬性

在WPF上是一種特殊的值存儲手段,主要是利用鍵值對原理,通過全局維護的方式進行儲存和更新。

主要作用于控件的屬性值儲存,讀取。在XAML中會有特殊的處理,包括讀取,存儲,都是有特殊的對待。

使用方式主要是可讀可寫,隻讀兩種。

當你實作一個依賴屬性,其派生類(依賴屬性必須是繼承DependencyObject類,才能夠編寫)是可以對其值複寫。

由于依賴屬性并不是CRL屬性,是以是有特殊的寫法,但是依賴屬性是通過CRL屬性進行值的更新和擷取。

實作方式

依賴屬性是具有特殊寫法。

由public static readyonly 作範圍限制。并且是通過DependencyProperty類的靜态注冊方法進行注冊。

class NewGrid : Grid
    {
        public static readonly DependencyProperty ObjectProperty = DependencyProperty.Register("Object", typeof(object), typeof(NewGrid), new PropertyMetadata(null, new PropertyChangedCallback(OnValueChanged)));

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
                MessageBox.Show(e.NewValue.ToString());
        }

        public object Object
        {
            get
            {
                return GetValue(ObjectProperty);
            }
            set
            {
                SetValue(ObjectProperty, value);
            }
        }
        
    }      

 很明顯,register的方法的形參很多,而且對于依賴屬性的名稱也是有要求的。

簡單的解釋一下register的形參,第一位是注冊時的名稱,第二位依賴屬性存儲時值的類型,第三位依賴屬性是屬于誰,第四位是一個中繼資料其中分别是 第一位 初始值,第二個是當依賴屬性接收到新值時發生的事件

這裡面非常特别注意的是:依賴屬性的變量結尾是Property,依賴屬性的注冊名稱是變量名除去Property的部分,依賴屬性的值讀寫是通過CRL屬性實作,并且名稱是和注冊用的名稱相同。

當我們完成一次正确的依賴屬性編寫後,xaml上使用時會正确的識别類型,存儲的值。

例如我們将上文的代碼稍微修改成

public enum ObjectSourceTypeEnum
    {
        String = 1,
        Object = 2,
        Int = 3,
        Doubble = 4,

    }
    class NewGrid : Grid
    {
        public static readonly DependencyProperty ObjectSourceTypeProperty = DependencyProperty.Register("ObjectSourceType", typeof(ObjectSourceTypeEnum), typeof(NewGrid), new PropertyMetadata(ObjectSourceTypeEnum.String, new PropertyChangedCallback(OnValueChanged)));

        private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue != null)
                MessageBox.Show(e.NewValue.ToString());
        }

        public ObjectSourceTypeEnum ObjectSourceType
        {
            get
            {
                return (ObjectSourceTypeEnum)GetValue(ObjectSourceTypeProperty);
            }
            set
            {
                SetValue(ObjectSourceTypeProperty, value);
            }
        }

    }      

在xaml中則會正确識别

WPF 依賴屬性

同時,當你使用依賴屬性時,也會帶來一個好處就是使用綁定。

<Window.Resources>
        <ObjectDataProvider x:Key="value" MethodName="GetValues" ObjectType="{x:Type sys:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type Type="{x:Type local:ObjectSourceTypeEnum}"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <local:NewGrid x:Name="grid"   ObjectSourceType="{Binding ElementName=combobox,Path=SelectedItem}" >
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"/>
            <RowDefinition Height="auto"/>
        </Grid.RowDefinitions>
        <TextBlock VerticalAlignment="Top" HorizontalAlignment="Left" Text="{Binding  ElementName=grid,Path=ObjectSourceType}"/>
        <ComboBox x:Name="combobox" Grid.Row="1"  ItemsSource="{Binding Source={StaticResource value}}"  SelectedIndex="0"  />
    </local:NewGrid>      
WPF 依賴屬性