Wp8:如何清除枢轴内的列表框

时间:2014-11-24 07:58:26

标签: c# wpf xaml windows-phone-8 listbox

我有一个支点,在pivotitem我有一些列表框,里面有一些控件。 在选择更改事件时,我需要使用来自服务调用的列表更新listBox。 因此,在每个选择更改事件的枢轴我正在进行服务调用以相应地更新列表框。 我的xaml是:

 <Grid x:Name="LayoutRoot" Background="#FF641E7B">
    <phone:Pivot Title="ADMIN" x:Name="RequestsPivot" SelectionChanged="RequestsPivot_SelectionChanged" Foreground="White">
        <!--Pivot item one-->
        <phone:PivotItem Header="Request Queue" Foreground="White">
            <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Margin="5,0,5,0" >
                <ListBox x:Name="requestList" Width="400" Margin="10">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <!-- TextBlock x:Name="RequestID" Text="1" Width="50"/-->
                                <StackPanel Orientation="Vertical" Width="400">
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="Name : " FontWeight="Bold" 
                                        Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
                                        <TextBlock Text="{Binding DisplayName}" TextWrapping="Wrap" Width="400"
                                        Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
                                    </StackPanel>

                                    <StackPanel>
                                        <TextBlock Text=""/>
                                    </StackPanel>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

                </ListBox>

            </Grid>
        </phone:PivotItem>
        <!--Pivot item two-->
        <phone:PivotItem Header="Training Queue" Foreground="White">
            <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch"  Margin="5,0,5,0" >
                <ListBox x:Name="trainingList" Width="400" Margin="10">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <StackPanel Orientation="Vertical" Width="400">
                                    <StackPanel Orientation="Horizontal">
                                        <TextBlock Text="Name : " FontWeight="Bold"
                                        Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
                                        <TextBlock Text="{Binding DisplayName}" TextWrapping="Wrap" 
                                        Width="400" Style="{StaticResource ResourceKey=MSFTGuidelines_TextBlock}"/>
                                    </StackPanel>

                                    <StackPanel>
                                        <TextBlock Text=""/>
                                    </StackPanel>
                                </StackPanel>
                            </StackPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>

                </ListBox>

            </Grid>
        </phone:PivotItem>
</phone:Pivot>

</Grid>

当用户快速浏览枢轴时,显示的列表是旧值,然后它会快速更新为新值。所以我已经清除了列表。 但我得到一个例外 只读操作不支持操作 我选择更改的代码是

    private void RequestsPivot_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

        try
        {
            //clearing the list before service call 
            if (reqList.Count != 0 && reqList != null)
            {
                //reqList is the object List that has the 
                //response after service call 
                reqList.Clear();
                switch (RequestsPivot.SelectedIndex)
                {
                    case 0: this.requestList.Items.Clear();
                        break;
                    case 1: this.trainingList.Items.Clear();
                        break;

                }
            }
            //Setting status for making service call
            switch (RequestsPivot.SelectedIndex)
            {
                case 0: status = QueueStatus.AllRequests;
                    break;
                case 1: status = QueueStatus.Training;
                    break;

            }
            //funtion for service call and this function also binds the requestList 
            //and trainingList with reqList
            fetchData();
        }
        catch (Exception exp)
        {
            MessageBox.Show(exp.Message);
        }


    }

0 个答案:

没有答案