1

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

在 ListBox 上,您可以在 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>

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

4

3 回答 3

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 将是您用于扩展器项目的任何东西......

这对我来说很好

希望这可以帮助!

祝你好运!

于 2013-01-19T23:01:09.373 回答
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
        ...
    }
于 2013-01-10T21:53:54.653 回答
0

您可以通过列表框 selectionchanged 或 expanderview 扩展事件获取选定的值。对于列表框:

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

希望这可以帮助!

于 2013-02-19T12:32:30.307 回答