Silverlight 利用Tag在TextBox绑定中传递参数

假如我们要实现这样的功能:在DataGrid中有个TextBox,可以手工录入数据,并在用户按回车键时,把数据A传递到后台做处理,参数是B。

那么在XAML里可以这样写:

<DataGrid ItemsSource="{Binding}">

....

<TextBox KeyDown="TextBox_KeyDown" Tag="{Binding B}"/>

</DataGrid>

 

在CodeBehind里这么处理就可以了:

 private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)//触发了enter键
            {
                TextBox tb = (TextBox)sender;
                if (tb.Tag != null)
                {
                    //这里可以得到 tb.Tag.ToString().Trim() = B

                   // 也可以得到:tb.Text.Trim() = A

                }
            }
           
        }

 

太帅了!

 

你可能感兴趣的:(datagrid,object,null,silverlight,textbox,binding)