从数据绑定的 ListBox 中获取 TextBox 的值 [英] Getting the values of TextBox from a data bound ListBox

查看:21
本文介绍了从数据绑定的 ListBox 中获取 TextBox 的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从列表框所选项目中获取值.请注意,数据模板在数据绑定中.这是 xaml:

I need to get the values from Listbox selected items. Note that, the data templates are in data bound. here is the xaml:

<ListBox Name="AppointmentResultsData" ItemsSource="{Binding}" Height="650" Width="480" Margin="24,0,0,0" Foreground="#CBF7FA" SelectionChanged="AppointmentResultsData_SelectionChanged">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition/>
                            </Grid.ColumnDefinitions>
                            <Grid.RowDefinitions>
                                <RowDefinition/>
                                <RowDefinition/>
                                <RowDefinition/>
                            </Grid.RowDefinitions>
                            <TextBlock Text="{Binding Path=Subject, Mode=TwoWay}" TextWrapping="Wrap" FontSize="30" Grid.Column="0" Grid.Row="1"/>
                            <Grid Grid.Column="0" Grid.Row="2">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition/>
                                    <ColumnDefinition/>
                                </Grid.ColumnDefinitions>
                                <Grid.RowDefinitions>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                    <RowDefinition/>
                                </Grid.RowDefinitions>
                                <TextBlock Text="{Binding Path=Account.Name}" Grid.Column="0" Grid.Row="1" FontSize="28"/>
                                <TextBlock Text="Start : " Grid.Column="0" FontSize="22" Grid.Row="2"/>
                                <TextBlock Text="{Binding Path=StartTime}" FontSize="22" Grid.Column="1" Grid.Row="2"/>
                                <TextBlock Text="End : " Grid.Column="0" Grid.Row="3" FontSize="22"/>
                                <TextBlock Text="{Binding Path=EndTime}" Grid.Column="1" FontSize="22" Grid.Row="3"/>
                                <TextBlock Text="Location : " Grid.Column="0" Grid.Row="4" FontSize="22"/>
                                <TextBlock Text="{Binding Path=Location}" Grid.Column="1" FontSize="22" Grid.Row="4"/>
                                <TextBlock Text="Status : " Grid.Column="0" FontSize="22" Grid.Row="5"/>
                                <TextBlock Text="{Binding Path=Status}" Grid.Column="1" FontSize="22" Grid.Row="5"/>
                            </Grid>
                            <TextBlock Text="    "/>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

我需要选择更改事件中文本框的值.我试过这样...

I need values of the textboxes in selection changed event.I have tried like this...

private void AppointmentResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        //SelectedEvent seleted = AppointmentResultsData.SelectedItem as SelectedEvent;
        if (AppointmentResultsData.SelectedIndex == -1)
            return;

        ListBoxItem currentSelectedListBoxItem = this.AppointmentResultsData.ItemContainerGenerator.ContainerFromIndex(AppointmentResultsData.SelectedIndex) as ListBoxItem;

        if (currentSelectedListBoxItem == null)
            return;

        // Iterate whole listbox tree and search for this items
        TextBox nameBox = helperClass.FindDescendant<TextBox>(currentSelectedListBoxItem);
        TextBlock nameBlock = helperClass.FindDescendant<TextBlock>(currentSelectedListBoxItem);
        MessageBox.Show(nameBlock.Text + " " + nameBox.Text);
    }

但是没有用!

推荐答案

解决了!

private void AppointmentResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        var listBoxItem = AppointmentResultsData.ItemContainerGenerator.ContainerFromIndex(AppointmentResultsData.SelectedIndex) as ListBoxItem;
        var txtBlk = FindVisualChildByType<TextBlock>(listBoxItem, "txtLocation");

        MessageBox.Show(txtBlk.Text);
    }




T FindVisualChildByType<T>(DependencyObject element, String name) where T : class
    {
        if (element is T && (element as FrameworkElement).Name == name)
            return element as T;
        int childcount = VisualTreeHelper.GetChildrenCount(element);
        for (int i = 0; i < childcount; i++)
        {
            T childElement = FindVisualChildByType<T>(VisualTreeHelper.GetChild(element, i), name);
            if (childElement != null)
                return childElement;
        }
        return null;
    } 

这篇关于从数据绑定的 ListBox 中获取 TextBox 的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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