简体   繁体   English

如何从ListViewItem的DataTemplate中的TextBox获取文本

[英]How to get text from TextBox inside ListViewItem's DataTemplate

I don't know how to get text from "firstBox" and "secondBox" after button click. 单击按钮后,我不知道如何从“ firstBox”和“ secondBox”中获取文本。

<ListView.ItemTemplate>
    <DataTemplate>
      <Grid>
       <!-- some code -->
       <TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Data}" VerticalAlignment="Top" Height="18" Width="100" FontSize="13.333" Margin="162,9,0,0"/>
       <TextBlock HorizontalAlignment="Left" Margin="0,35,0,0" TextWrapping="Wrap" Text="{Binding D_gospodarzy}" FontSize="14.667" VerticalAlignment="Top" Height="59" Width="100"/>
       <TextBlock HorizontalAlignment="Center" Margin="268,35,7,0" TextWrapping="Wrap" Text="{Binding D_gosci}" FontSize="14.667" VerticalAlignment="Top" Width="100" Height="59"/>
       <TextBox x:Name="firstBox" ... />
       <Button Content="Click" " Click="Button_Click_1"/>
       <TextBox x:Name="secondBox" ... />
     </Grid>
   </DataTemplate>
</ListView.ItemTemplate>

I get only the object 我只得到对象

private void Button_Click_1(object sender, RoutedEventArgs e)
{
   var myobject = (sender as Button).DataContext;            
}

There are cuple of ways to do it, for example you can traverse the VisualTree of clicked button's parent and retrive TextBox with the name you want. 有很多方法可以做到这一点,例如,您可以遍历单击按钮的父级的VisualTree并使用所需的名称检索TextBox In this case, I would take advantage of an extension method written by yasen in this answer . 在这种情况下,我将利用yasen在此答案中编写的扩展方法。

Then it can look for example like this: 然后,它看起来像这样:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
    var parent = (sender as Button).Parent;
    TextBox firstOne = parent.GetChildrenOfType<TextBox>().First(x => x.Name == "firstBox");
    Debug.WriteLine(firstOne.Text);
}

Remember to put an extension method somewhere in a static class: 请记住将扩展方法放在静态类中的某个位置:

public static class Extensions
{
    public static IEnumerable<T> GetChildrenOfType<T>(this DependencyObject start) where T : class
    {
          // rest of the code

Here's how to get the text.. 这是获取文本的方法。

 String text1 = firstBox.Text;
 String text2 = secondBox.Text;

note: firstBox and secondBox must be class members to use them in different class methods. 注意: firstBoxsecondBox必须是类成员,才能在不同的类方法中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在代码隐藏中从DataTemplate内部的TextBlock获取文本 - How to get the Text from TextBlock inside a DataTemplate in code-behind 如何在数据模板中获取文本? - How to get the text with in the datatemplate? 如何在mvvm模型中的wpf中为Datagrid的数据模板内部的文本框提供ID,以便我可以区分文本框 - How to provide the ID to the Textbox that is inside the Datagrid's datatemplate in wpf in mvvm model, So that I can differentiate the textbox 从DataTemplate(WPF)内的文本框中获取值 - Obtain the value from textbox inside of a DataTemplate (WPF) 如何从 ListViewItem 获取 ProgressRing? - How to get ProgressRing from ListViewItem? 对DataTemplate内部的TextBox的引用 - Reference to a TextBox inside a DataTemplate XAML从ListViewItem获取项目文本值 - XAML Get Item text value from ListViewItem 如何从Windows控件的文本框中获取文本? 我怎么知道这是一个文本框? - How to get text from a windows control's textbox? and how do I know it's a textbox? Listview内的TextBox-在TextBox上获取ListViewItem TextChanged - TextBox inside Listview - getting ListViewItem on TextBox TextChanged 如何在WPF中查找由ListViewItem DataTemplate生成的元素 - how to find elements that are generated by a ListViewItem DataTemplate in WPF
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM