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;

        }

現在選択されているアイテムのオブジェクトがあり、すべてのクラス変数にアクセスできます。例(StartTime など)

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

もちろん、名前を追加する必要がありTextBoxますTextBlock

<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 に答える