我偶爾遇到問題。這裏是一個簡單的例子:爲什麼DependencyProperty會覆蓋正常的屬性綁定行爲?
XAML代碼:
<Window x:Class="WPFProperties.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
x:Name="root">
<Grid>
<TextBox Text="{Binding Text,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged,ElementName=root}"/>
</Grid>
</Window>
代碼後面:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//// Using a DependencyProperty as the backing store for Text. This enables animation, styling, binding, etc...
//public static readonly DependencyProperty TextProperty =
// DependencyProperty.Register("Text", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null));
private string _text;
public string Text
{
get { return _text; }
set
{
if (this._text == value) return;
_text = value;
this.DoSomething();
}
}
private void DoSomething()
{
// Do Someting
}
}
方法DoSomething的()可以被調用,在文本框打字時。但是,一旦我取消註釋依賴屬性「文本」,它永遠不會被調用。
注:我知道依賴屬性的基本用法。
檢查此http://www.wpftutorial.net/DependencyProperties.html。也許你會知道一些關於依賴屬性的東西 – blindmeis