C# 在expanderView上获取所选项目

C# 在expanderView上获取所选项目,c#,xaml,windows-phone-7.1,onclicklistener,expander,C#,Xaml,Windows Phone 7.1,Onclicklistener,Expander,我正在使用ExpanderView在我的应用程序中显示一些数据。但我在尝试找出如何在选择ExpanderViewItem后获取其数据时遇到了一些困难 在列表框中,您可以在xaml代码中调用SelectionChanged=“yourFunction”。。但是对于expanderview,我不知道怎么做 这是我的扩展器XAML代码: <!--Custom header template--> <DataTemplate x:Key="CustomHeaderTempla

我正在使用ExpanderView在我的应用程序中显示一些数据。但我在尝试找出如何在选择ExpanderViewItem后获取其数据时遇到了一些困难

在列表框中,您可以在xaml代码中调用SelectionChanged=“yourFunction”。。但是对于expanderview,我不知道怎么做

这是我的扩展器XAML代码:

 <!--Custom header template-->
    <DataTemplate x:Key="CustomHeaderTemplate">
        <TextBlock Text="" FontSize="28" />            
    </DataTemplate>

    <!--Custom expander template-->
    <DataTemplate x:Key="CustomExpanderTemplate">

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="auto" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Rectangle Width="400" Height="60" Fill="#FFF1F1F1" HorizontalAlignment="Stretch" StrokeThickness="0" Grid.Row="0" Grid.Column="0" />
            <TextBlock Text="{Binding procedureName}" FontSize="30" Foreground="#FF00457C" FontWeight="Normal" Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="10,0,0,0" />

        </Grid>

    </DataTemplate>

    <!--Custom expander items template-->
    <DataTemplate x:Key="ExpanderViewItems" >

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="Auto" />
                <RowDefinition Height="15" />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>

            <Image Source="{Binding flagIcon}" Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" />
            <TextBlock FontSize="26" Text="{Binding N}" Foreground="Black" FontWeight="Normal" Grid.Row="0" Grid.Column="1"/>
            <TextBlock FontSize="20" Text="{Binding RNG}" Foreground="Black" FontWeight="Normal" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="2"/>                
            <TextBlock FontSize="26" Text="{Binding ValueAndUnit}" Foreground="Black" FontWeight="Medium" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/>
            <TextBlock FontSize="18" Text="{Binding COM}" Foreground="Black" FontWeight="Normal" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" TextWrapping="Wrap" />
            <Line StrokeThickness="1" Stroke="#C4C6CC" Stretch="Fill" X1="0" X2="1" Y1="0" Y2="0" VerticalAlignment="Center" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" />
        </Grid>
    </DataTemplate>

<!--Listbox Containing ExpanderViews-->
            <ListBox Name="testsList" Grid.Row="3" Grid.Column="0" >
                <ListBox.ItemTemplate>
                    <DataTemplate>

                        <!--ExpanderView-->
                        <toolkit:ExpanderView Header="{Binding}"                                                   
                                              HeaderTemplate="{StaticResource CustomHeaderTemplate}"
                                              Expander="{Binding}"
                                              ExpanderTemplate="{StaticResource CustomExpanderTemplate}"
                                              x:Name="expander" 
                                              FontSize="36" 
                                              Foreground="#FF00457C" 
                                              ItemsSource="{Binding testItems}"
                                              ItemTemplate="{StaticResource ExpanderViewItems}" >                          
                        </toolkit:ExpanderView>

                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

我真的非常感谢任何正确方向的帮助!这似乎是一个在网络上很难回答的问题。

您可以使用列表框上的“点击”事件:

在XAML文件中添加一个tap事件列表器:

<!--Listbox Containing ExpanderViews-->
        <ListBox Name="testsList" Grid.Row="3" Grid.Column="0" Tap="testsList_Tap" >
            <ListBox.ItemTemplate>
                <DataTemplate>

                    <!--ExpanderView-->
                    <toolkit:ExpanderView Header="{Binding}"
                    ...      

@Frederik我已经使用ListBox的SelectionChanged事件实现了您在上面代码中所做的事情——它对我来说仍然很好。 我的头撞在墙上已经有一段时间了,但最终还是设法解决了。首先,对于ItemTemplate,我已确保将模板放置在ListBoxItem元素中,如下所示:

<DataTemplate x:Key="ExpanderViewItems" >
<ListBoxItem DataContext="{Binding}" Tap="ListBoxItem_Tap_1">
<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="15" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="auto" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>

        <Image Source="{Binding flagIcon}" Grid.Row="0" Grid.RowSpan="3" Grid.Column="0" />
        <TextBlock FontSize="26" Text="{Binding N}" Foreground="Black" FontWeight="Normal" Grid.Row="0" Grid.Column="1"/>
        <TextBlock FontSize="20" Text="{Binding RNG}" Foreground="Black" FontWeight="Normal" HorizontalAlignment="Right" Grid.Row="0" Grid.Column="2"/>                
        <TextBlock FontSize="26" Text="{Binding ValueAndUnit}" Foreground="Black" FontWeight="Medium" HorizontalAlignment="Right" Grid.Row="1" Grid.Column="1" Grid.ColumnSpan="2"/>
        <TextBlock FontSize="18" Text="{Binding COM}" Foreground="Black" FontWeight="Normal" Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" TextWrapping="Wrap" />
        <Line StrokeThickness="1" Stroke="#C4C6CC" Stretch="Fill" X1="0" X2="1" Y1="0" Y2="0" VerticalAlignment="Center" Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="3" />
    </Grid>
</ListBoxItem>
</DataTemplate>
当然,ExpanderItemModel将是您用于扩展项的任何东西

这对我很管用

希望这有帮助


祝你好运

您可以通过listbox selectionchanged或expanderview expanded events获取所选值。 对于列表框:

    private void lstExams_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            Model.ExamTitles data = (sender as ListBox).SelectedItem as Model.ExamTitles;
           }
     }
这里ExamTitles是一个包含集合的类

对于expanderview已展开

    private void ExpanderView_Expanded(object sender, RoutedEventArgs e)
    {
        ExpanderView expview = (sender as ExpanderView);
        Model.ExamTitles data = expview.Header as Model.ExamTitles;
    }
希望这有帮助

    private void lstExams_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (e.AddedItems.Count > 0)
        {
            Model.ExamTitles data = (sender as ListBox).SelectedItem as Model.ExamTitles;
           }
     }
    private void ExpanderView_Expanded(object sender, RoutedEventArgs e)
    {
        ExpanderView expview = (sender as ExpanderView);
        Model.ExamTitles data = expview.Header as Model.ExamTitles;
    }