2
對於一些目的,我想只要一個多行TextBox的LineCount
改變
我想我可以做到這一點,如果我依賴項屬性綁定到TextBox.LineCount
下面引發一個事件是代碼示例
如何將屬性綁定到WPF中的僅限getter的CLR屬性?
XAML
<local:MyTextBox Margin="5,65,5,5" x:Name="txtBox"
AcceptsReturn="True" AcceptsTab="False"
MyProperty="{Binding LineCount, RelativeSource={RelativeSource Self}}" />
代碼隱藏
public class MyTextBox : TextBox
{
public MyTextBox()
{
DataContext = this;
}
public int MyProperty
{
get
{
return (int)this.GetValue(LineCountProperty);
}
set
{
this.SetValue(LineCountProperty, value);
}
}
public static readonly DependencyProperty LineCountProperty =
DependencyProperty.Register(
"MyProperty",
typeof(int),
typeof(TextBox),
new FrameworkPropertyMetadata(
new PropertyChangedCallback(
(dp, args) => { MessageBox.Show(args.NewValue.ToString()); })
));
}
只有在表格加載後,而不是行數改變後才顯示消息框。
但是,如果我將MyProperty
的綁定從TextBox的LineCount
更改爲Text
每次文本更改時都會觸發PropertyChangedEvent。
我相信我可以從TextChanged eventhandler中觸發一些自定義的LineCount更改事件,但是我想通過DepedencyProperties
來做到這一點,因爲我相信它會更有效率。
難道你不能綁定到任何屬性?我認爲只有目標必須是DP。 – Alan 2013-02-27 14:47:44