使用DependencyProperty添加属性

在UWP或WPF开发当中,通常能需要自定义添加属性。可以使用 DependencyProperty实现。

cs code:

using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace XamlApp.UWP.CustomProperties
{
     public class WebViewPorpeties
     {
         public static readonly DependencyProperty SourceStringProperty =
             DependencyProperty.RegisterAttached("SourceString", typeof(string), 
                 typeof(WebViewPorpeties), new PropertyMetadata("", OnSourceStringChanged));

         public static string GetSourceString(DependencyObject obj) 
         { return obj.GetValue(SourceStringProperty).ToString(); }
         public static void SetSourceString(DependencyObject obj, string value) 
         { obj.SetValue(SourceStringProperty, value); }

         private static void OnSourceStringChanged(DependencyObject d,
              DependencyPropertyChangedEventArgs e)
         {
            WebView wv = d as WebView;
            if (wv != null)
            {
                //可在此调用方法
                wv.NavigateToString(e.NewValue.ToString());
            }
         }
     }
}

Xaml code:

xmlns:properties="using:XamlApp.UWP.CustomProperties"
<WebView properties:WebViewProperties.SourceString="{Binding dataString}"/>

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s