咱們經過下面的這幅圖,簡單介紹一下WPF屬性系統對依賴屬性操做的基本步驟:ide
借用一個常見的圖例,介紹一下WPF屬性系統對依賴屬性操做的基本步驟: 函數
那麼應該如何使用這些功能呢?
性能
前面咱們講了基本的流程,下面咱們就用一個小的例子來進行說明:測試
XAML的代碼以下:優化
<Window x:Class="WpfApp1.WindowValid" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title=" WindowValid " Height="300" Width="400"> <Grid> <StackPanel> <Button Name="btnDPTest" Click="btnDPTest_Click" >屬性值執行順序測試</Button> </StackPanel> </Grid> </Window>
C#的代碼以下:動畫
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Windows.Threading; using WpfApp1.Models; namespace WpfApp1 { /// <summary> /// WindowThd.xaml 的交互邏輯 /// </summary> public partial class WindowValid: Window { public WindowValid () { InitializeComponent(); } private void btnDPTest_Click(object sender, RoutedEventArgs e) { SimpleDP test = new SimpleDP(); test.ValidDP = 1; } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace WpfApp1.Models { public class SimpleDP : DependencyObject { public static readonly DependencyProperty ValidDPProperty = DependencyProperty.Register("ValidDP", typeof(int), typeof(SimpleDP), new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnValueChanged), new CoerceValueCallback(CoerceValue)), new ValidateValueCallback(IsValidValue)); public int ValidDP { get { return (int)GetValue(ValidDPProperty); } set { SetValue(ValidDPProperty, value); } } private static void OnValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Console.WriteLine("當屬性值的OnValueChanged方法被調用,屬性值爲: {0}", e.NewValue); } private static object CoerceValue(DependencyObject d, object value) { Console.WriteLine("當屬性值的CoerceValue方法被調用,屬性值強制爲: {0}", value); return value; } private static bool IsValidValue(object value) { Console.WriteLine("當屬性值的IsValidValue方法被調用,對屬性值進行驗證,返回bool值,若是返回True表示嚴重經過,不然會以異常的形式拋出: {0}", value); return true; } } }
結果以下:spa
當ValidDP屬性變化以後,PropertyChangeCallback就會被調用。能夠看到結果並無徹底按照咱們先前的流程先 Coerce後Validate的順序執行,有多是WPF內部作了什麼特殊處理,當屬性被修改時,首先會調用Validate來判斷傳入的value是 否有效,若是無效就不繼續後續的操做,這樣能夠更好的優化性能。從上面的結果上看出,CoerceValue後面並無當即ValidateValue, 而是直接調用了PropertyChanged。這是由於前面已經驗證過了value,若是在Coerce中沒有改變value,那麼就不用再驗證了。如 果在 Coerce中改變了value,那麼這裏還會再次調用ValidateValue操做,和前面的流程圖執行的順序同樣,在最後咱們會調用 ValidateValue來進行最後的驗證,這就保證最後的結果是咱們但願的那樣了。code
上面簡單介紹了處理流程,下面咱們就以一個案例來具體看一看上面的流程到底有沒有出入。xml
依賴屬性代碼文件以下:blog
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; namespace WpfApp1.Controls { class MyValiDP:System.Windows.Controls.Control { //註冊Current依賴屬性,並添加PropertyChanged、CoerceValue、ValidateValue的回調委託 public static readonly DependencyProperty CurrentValueProperty = DependencyProperty.Register( "CurrentValue", typeof(double), typeof(MyValiDP), new FrameworkPropertyMetadata( Double.NaN, FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnCurrentValueChanged), new CoerceValueCallback(CoerceCurrentValue) ), new ValidateValueCallback(IsValidValue) ); //屬性包裝器,經過它來暴露Current的值 public double CurrentValue { get { return (double)GetValue(CurrentValueProperty); } set { SetValue(CurrentValueProperty, value); } } //註冊Min依賴屬性,並添加PropertyChanged、CoerceValue、ValidateValue的回調委託 public static readonly DependencyProperty MinValueProperty = DependencyProperty.Register( "MinValue", typeof(double), typeof(MyValiDP), new FrameworkPropertyMetadata( double.NaN, FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnMinValueChanged), new CoerceValueCallback(CoerceMinValue) ), new ValidateValueCallback(IsValidValue)); //屬性包裝器,經過它來暴露Min的值 public double MinValue { get { return (double)GetValue(MinValueProperty); } set { SetValue(MinValueProperty, value); } } //註冊Max依賴屬性,並添加PropertyChanged、CoerceValue、ValidateValue的回調委託 public static readonly DependencyProperty MaxValueProperty = DependencyProperty.Register( "MaxValue", typeof(double), typeof(MyValiDP), new FrameworkPropertyMetadata( double.NaN, FrameworkPropertyMetadataOptions.None, new PropertyChangedCallback(OnMaxValueChanged), new CoerceValueCallback(CoerceMaxValue) ), new ValidateValueCallback(IsValidValue) ); //屬性包裝器,經過它來暴露Max的值 public double MaxValue { get { return (double)GetValue(MaxValueProperty); } set { SetValue(MaxValueProperty, value); } } //在CoerceCurrent加入強制判斷賦值 private static object CoerceCurrentValue(DependencyObject d, object value) { MyValiDP g = (MyValiDP)d; double current = (double)value; if (current < g.MinValue) current = g.MinValue; if (current > g.MaxValue) current = g.MaxValue; return current; } //當Current值改變的時候,調用Min和Max的CoerceValue回調委託 private static void OnCurrentValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { d.CoerceValue(MinValueProperty); d.CoerceValue(MaxValueProperty); } //當OnMin值改變的時候,調用Current和Max的CoerceValue回調委託 private static void OnMinValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { d.CoerceValue(MaxValueProperty); d.CoerceValue(CurrentValueProperty); } //在CoerceMin加入強制判斷賦值 private static object CoerceMinValue(DependencyObject d, object value) { MyValiDP g = (MyValiDP)d; double min = (double)value; if (min > g.MaxValue) min = g.MaxValue; return min; } //在CoerceMax加入強制判斷賦值 private static object CoerceMaxValue(DependencyObject d, object value) { MyValiDP g = (MyValiDP)d; double max = (double)value; if (max < g.MinValue) max = g.MinValue; return max; } //當Max值改變的時候,調用Min和Current的CoerceValue回調委託 private static void OnMaxValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { d.CoerceValue(MinValueProperty); d.CoerceValue(CurrentValueProperty); } //驗證value是否有效,若是返回True表示驗證經過,不然會提示異常 public static bool IsValidValue(object value) { Double v = (Double)value; return (!v.Equals(Double.NegativeInfinity) && !v.Equals(Double.PositiveInfinity)); } } }
XAML代碼以下:
<Window x:Class="WpfApp1.WindowProcess" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApp1.Controls" Title="WindowProcess" Height="400" Width="500"> <Grid> <StackPanel Orientation="Vertical"> <local:MyValiDP x:Name="myValiDP1" MaxValue="500" MinValue="0" /> <Label Content="能夠設置最小值爲0和最小大值爲500" Height="30"/> <StackPanel Orientation="Horizontal" Height="60"> <Label Content="當前值爲 : "/> <Label Background="Yellow" BorderBrush="Black" BorderThickness="1" IsEnabled="False" Content="{Binding ElementName=myValiDP1, Path=CurrentValue}" Height="25" VerticalAlignment="Top" /> </StackPanel> <WrapPanel > <Label Content="最小值" /> <Slider x:Name="sliderMin" Minimum="-200" Maximum="100" Width="300" ValueChanged="sliderMin_ValueChanged" SmallChange="10" /> <Label Content="{Binding ElementName=sliderMin, Path=Value}" /> </WrapPanel> <WrapPanel > <Label Content="最大值" /> <Slider x:Name="sliderMax" Minimum="200" Maximum="800" Width="300" ValueChanged="sliderMax_ValueChanged" SmallChange="10" /> <Label Content="{Binding ElementName=sliderMax, Path=Value}" /> </WrapPanel> </StackPanel> </Grid> </Window>
C#代碼以下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace WpfApp1 { /// <summary> /// WindowProcess.xaml 的交互邏輯 /// </summary> public partial class WindowProcess : Window { public WindowProcess() { InitializeComponent(); //設置Current的值 myValiDP1.CurrentValue = 100; } private void sliderMin_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { //設置Current的值 myValiDP1.CurrentValue = (int)sliderMin.Value; } private void sliderMax_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { //設置Current的值 myValiDP1.CurrentValue = (int)sliderMax.Value; } } }
示例效果以下圖。
在上面的例子中,一共有三個依賴屬性相互做用——CurrentValue、MinValue和MaxValue,這些屬性相互做 用,但它們的規則是MinValue≤CurrentValue≤MaxValue。根據這個規則,當其中一個依賴屬性變化時,另外兩個依賴 屬性必須進行適當的調整,這裏咱們要用到的就是CoerceValue這個回調委託,那麼實現起來也很是的簡單,註冊MaxValue的時候加入 CoerceValueCallback,在CoerceMaxValue函數中作處理:若是Maximum的值小於MinValue,則使 MaxValue值等於MinValue;同理在CurrentValue中也加入了CoerceValueCallback進行相應的強制 處理。而後在MinValue的ChangedValueCallback被調用的時候,調用CurrentValue和MaxValue的 CoerceValue回調委託,這樣就能夠達到相互做用的依賴屬性一變應萬變的」千機變「。
換句話說,當相互做用的幾個依賴屬性其中一個發生變化時,在它的PropertyChangeCallback中調用受它影響的依賴屬性的CoerceValue,這樣才能保證相互做用關係的正確性。 前面也提升ValidateValue主要是驗證該數據的有效性,最設置了值之後都會調用它來進行驗證,若是驗證不成功,則拋出異常。