0

我需要从列表框选定项中获取值。请注意,数据模板是数据绑定的。这是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>

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

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);
    }

但它没有用!

4

3 回答 3

1

解决了!

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;
    } 
于 2012-07-11T06:09:56.817 回答
0

假设您有一个已绑定到列表框的类(MyClass)对象列表

将处理程序手势侦听器点击添加到数据模板

在处理程序中这样做:

private void ItemClickedEventHandler(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {                   
           MyClass clickedMyclass = (MyClass)((System.Windows.Controls.Grid)sender).DataContext;

        }

您拥有当前选定项目的对象,并且可以访问所有类变量。例如(开始时间等)

于 2012-07-10T11:23:13.340 回答
0

好吧,您将其转换为错误的类型,这应该可以:

private void AppointmentResultsData_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {  
      var listBoxItem = AppointmentResultsData.SelectedItem as ListBoxItem;
      TextBox nameBox = listBoxItem .FindName("nameYourTextBox") as TextBox;
      TextBlock nameBlock = dd.FindName("nameYourTextBlock") as TextBlock;
      MessageBox.Show(nameBlock.Text + " " + nameBox.Text);
     }

当然,您需要将名称添加到您的TextBoxTextBlock

<TextBlock x:Name="nameYourTextBlock Text="{Binding Path=Account.Name}" Grid.Column="0" Grid.Row="1" FontSize="28"/>

另外,我TextBox在您的XAML.

于 2012-07-10T12:34:54.577 回答