如何获取datagrid内的textBox的名称? [英] How to get name of textBox which is inside datagrid?

查看:72
本文介绍了如何获取datagrid内的textBox的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个dataGrid.里面是更多行.在一行中是一个文本框.所以我的问题是:如何从dtg1.SelectionChanged获取此文本框的名称?

I have created a dataGrid. Inside is more rows. In one row is a textBox. So my question is : How can I get name of this textbox from dtg1.SelectionChanged?

这是我的dataGrid结构的示例

this is example of my dataGrid structure

<DataGrid Grid.Row="4" Name="dtg1">
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Width="50" Visibility="Visible" Binding="{Binding Path=id}" />
        <DataGridTextColumn Header="Name" Width="1*" Binding="{Binding Path=name}" />
        ...
    </DataGrid.Columns>
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <Grid Height="49.2">
                <Grid.RowDefinitions>
                    <RowDefinition />
                </Grid.RowDefinitions>
                <Label Content="phone" FontWeight="Bold" />

                <TextBox Name="txtPhone" Grid.Column="1" Text="{Binding Path=phone}" />
                ....
            </Grid>
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
</DataGrid>

并且这里我叫dtg1.SelectionChanged

and  here I call dtg1.SelectionChanged

private void btn_Click(object sender, RoutedEventArgs e)
{
    if (dtg1.SelectedIndex > -1)
    {
        string s = txtPhone.Text; // <-- don't know name txtPhone !
    }
    else
    {
        MessageBox.Show("No row is selected.", "Info");
    }
}

推荐答案

如果使用MVVM,这种事情将更容易实现.

This sort of thing would be way easier to do if you used MVVM.

您的逻辑可以进入每行使用的视图模型(或包含绑定集合的视图模型).

Your logic could go into a viewmodel you use per row ( or a viewmodel containing the bound collection ).

那将是一个按钮,它根本不需要引用另一个控件,因为它只是在它自己的对象中设置了另一个属性.

That'd be a button and it wouldn't need to get a reference to another control at all because it'd just set another property in it's own object.

此示例适用于列,但应使您了解我的意思.

This sample works with columns, but should give you the idea of what I mean.

https://gallery.technet.microsoft.com/WPF-Command-and -Row-in-84635e1a

 


这篇关于如何获取datagrid内的textBox的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆