Luk*_*uke 2 c# data-binding silverlight binding
我有一个TextBoxes列表,它们绑定到不同的属性.
<TextBox Text="{Binding Name, Mode=TwoWay,ValidatesOnDataErrors=True, ValidatesOnExceptions=True, NotifyOnValidationError=True}" VerticalAlignment="Center" Margin="5" Width="300" Grid.Column="1" Grid.Row="1" LostFocus="TextBox_Validate"/>
Run Code Online (Sandbox Code Playgroud)
我想写一个处理程序,如
private void TextBox_Validate(object sender, RoutedEventArgs e)
{
var textBox = (sender as TextBox);
if(textBox!=null)
{
var propertyName = X; // Get propertyName textBox.Text is bound to.
CurrentDataContext.ValidateFields("Name"); // Name in this specific textBox
}
}
Run Code Online (Sandbox Code Playgroud)
是否有可能获得属性的名称,所以我不必编写许多不同的方法来做同样的事情?
我想这就是你想要的:
var expression = textBox.GetBindingExpression(TextBox.TextProperty);
if (expression != null && expression.ParentBinding != null)
{
var propertyName = expression.ParentBinding.Path.Path;
}
Run Code Online (Sandbox Code Playgroud)
编辑
或者你可以用BindingOperations.GetBinding如图所示这里.我不确定一种方式是否优于另一种方式.
| 归档时间: |
|
| 查看次数: |
1556 次 |
| 最近记录: |