为什么要再次调用转换器?

时间:2015-06-06 04:23:26

标签: c# xaml windows-phone-8 converter longlistselector

在我的Windows手机应用程序中,其中一个页面包含LongListSelector。在那LongListSelector我正在使用一些转换器。正常调用LongListSelector加载转换器时。但是,当我调用ScrollTo()方法滚动到LongListSelector的特定项时,将再次调用转换器。为什么要再次调用转换器? ScrollTo()方法做什么导致转换器再次被调用?

示例代码:

XAML:

<phone:LongListSelector ItemTemplate="{StaticResource LLSItemSource}"
                        Name="ChatListBox">

    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <Grid Background="Transparent"
                  Visibility="{Binding ID, Converter={StaticResource visibilityConverter}}"
                  Margin="0,6">

            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu>
                    <toolkit:MenuItem Header="delete"
                                      Click="ContextMenuItem_Click" />
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>

            <Border Background="Black"
                    HorizontalAlignment="Center"
                    Padding="30,5,30,8"
                    CornerRadius="20">
                <TextBlock Text="{Binding Date, Converter={StaticResource dateStringConverter}}"
                           Foreground="White"
                           FontSize="20" />
            </Border>
        </Grid>     
    </DataTemplate> 
</phone:LongListSelector.ItemTemplate>

CS:

private void ContextMenuItem_Click(object sender, RoutedEventArgs e)
{
    string header = (sender as MenuItem).Header.ToString();
    MessageModel selectedListBoxItem = (sender as MenuItem).DataContext as MessageModel;

    if (selectedListBoxItem == null)
        return;

    if (header == "delete")
    {
        DeleteItemByID(selectedListBoxItem.ID);

        if (ChatListBox.ItemsSource.Count > 0)
        {
            ChatListBox.ScrollTo(ChatListBox.ItemsSource[delIndex - 1]); // here I am scrolling to the last item that causes converters to be called again
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您为longlistselector项目编写了转换器。在加载选择器中的每个项目时,它会执行以转换给定模板上的值