在expandderView上获取所选项目

时间:2012-10-29 14:07:43

标签: c# xaml windows-phone-7.1 onclicklistener expander

我正在使用ExpanderView在我的应用中显示一些数据。但是我在尝试找到如何获取ExpanderViewItem数据后遇到了一些困难。

在ListBox上你可以在你的xaml代码中调用SelectionChanged =“yourFunction”..但对于expandderview我不知道怎么做?

这是我对扩展器的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>

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

3 个答案:

答案 0 :(得分:2)

@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>

一旦到位,请进入后面的代码,并在为ListBoxItem声明的Tap事件中使用如下内容:

ListBoxItem item = sender as ListBoxItem;
ExpanderItemModel model = item.DataContext as ExpanderItemModel;

当然,ExpanderItemModel将是您用于扩展器项目的任何内容......

这对我来说很好用

希望这有帮助!

祝你好运!

答案 1 :(得分:1)

您可以使用列表框中的“点按”事件:

在您的XAML文件中添加点击事件列表器:

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

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

在您的代码隐藏文件中,实现点击处理程序:

    private void testsList_Tap(object sender, System.Windows.Input.GestureEventArgs e)
    {
        someModel selectedItem = (someModel)this.testsList.SelectedItem;
        // Do something with your seleted data
        ...
    }

答案 2 :(得分:0)

您可以通过listbox selectionchanged或expandderview扩展事件获取所选值。  对于列表框:

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

这里的ExamTitles是一个包含集合的类

对于expandderview Expanded

    private void ExpanderView_Expanded(object sender, RoutedEventArgs e)
    {
        ExpanderView expview = (sender as ExpanderView);
        Model.ExamTitles data = expview.Header as Model.ExamTitles;
    }

希望这有帮助!