如何从文本框获取文本的ListViewItem的的DataTemplate内 [英] How to get text from TextBox inside ListViewItem's DataTemplate

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

问题描述

我不知道如何从firstBox和secondBox按钮,点击后得到的文本。

< ListView.ItemTemplate>
    <&DataTemplate的GT;
      <网格和GT;
       <! - 约code - >
       < TextBlock中的Horizo​​ntalAlignment =左TextWrapping =自动换行文本={绑定数据}VerticalAlignment =评出的HEIGHT =18WIDTH =100字号=13.333保证金=162,9,0, 0/>
       < TextBlock中的Horizo​​ntalAlignment =左保证金=0,35,0,0TextWrapping =自动换行文本={结合D_gospodarzy}字号=14.667VerticalAlignment =评出的HEIGHT =59WIDTH = 100/>
       < TextBlock中的Horizo​​ntalAlignment =中心保证金=268,35,7,0TextWrapping =自动换行文本={结合D_gosci}字号=14.667VerticalAlignment =评出的WIDTH =100HEIGHT = 59/>
       <文本框X:NAME =firstBox... />
       <按钮内容=点击点击=Button_Click_1/>
       <文本框X:NAME =secondBox... />
     < /网格和GT;
   < / DataTemplate中>
< /ListView.ItemTemplate>

我只得到对象

私人无效Button_Click_1(对象发件人,RoutedEventArgs E)
{
   VAR为MyObject =(发件人为按钮).DataContext;
}


解决方案

有办法做到这一点CUPLE,例如,你可以遍历点击按钮的父的的的VisualTree 的和retrive的文本框的与你想要的名称。在这种情况下,我会采取由亚森在这个答案写扩展方法的优势。

那看起来就例如是这样的:

私人无效Button_Click_1(对象发件人,RoutedEventArgs E)
{
    VAR父=(发件人为按钮).Parent;
    文本框firstOne = parent.GetChildrenOfType<&文本框GT;()第一。(X => x.Name ==firstBox);
    的Debug.WriteLine(firstOne.Text);
}

切记把扩展方法的地方在的静态的类:

公共静态类扩展
{
    公共静态的IEnumerable< T> GetChildrenOfType< T>(此DependencyObject开始),其中T:类
    {
          //将code休息

I don't know how to get text from "firstBox" and "secondBox" after button click.

<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. In this case, I would take advantage of an extension method written by yasen in this answer.

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

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

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