动态设置TextBox文本

GAr*_*ech 5 c# wpf xaml textbox

我有多个XAML TextBoxes,当TextBox使用C#动态检查TextBox调用方法的方法更改其中的值时,每个XAML 都会操作数组中的相应值.

    <TextBox x:Name="_0_0" TextChanged="_x_y_TextChanged"/>
    <TextBox x:Name="_0_1" TextChanged="_x_y_TextChanged"/>
    <TextBox x:Name="_0_2" TextChanged="_x_y_TextChanged"/>
    // And so on.....
Run Code Online (Sandbox Code Playgroud)

TextBox更改中的值时,每个操作数组中的相应值,使用动态检查TextBox已调用方法的C#方法.

    private void _x_y_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox current = (TextBox)sender;
        string currentname = current.Name;
        string rowstring = currentname.Substring(1, 1);

        string columnstring = currentname.Substring(3, 1);

        int row = Convert.ToInt32(rowstring);
        int column = Convert.ToInt32(columnstring);

        // I've then detected the name of the textbox which has called it...
Run Code Online (Sandbox Code Playgroud)

因此,此信息可用于动态存储来自TextBox相应数组索引中的信息 - 或者您想要使用它做什么......

然而,我的问题是:

如何创建一个在我的数组中使用索引位置的方法,调用相关的TextBox并更新其文本?

jam*_*eff 7

用于FindName(string)按名称查找文本框,如下所示(其中container包含所有文本框的控件):

private void UpdateTextBox(int row, int column, string text)
{
    TextBox textBox = container.FindName("_" + row + "_" + column) as TextBox;
    if(textbox != null)
    {
        textbox.Text = text;
    }
}
Run Code Online (Sandbox Code Playgroud)