2013-09-20 6 views
0

Я хотел бы отменить выбор продукта в LongListSelector программно, но я не уверен, как выполнить эту задачу. Я правильно отлаживаю выбор, используя обработчик события SelectionChanged. Моя проблема возникает, когда вы покидаете страницу, а затем возвращаетесь назад, я очищаю значения, используемые в событии SelectionChanged, потому что они могут меняться, но если мне нужно повторно выбрать элемент в LongListSelector, событие не срабатывает, потому что оно думает, что оно уже выбран.Как отменить выбор объекта LongListSelector программно

MainPage.xaml

<phone:PhoneApplicationPage.Resources> 

    <Style x:Key="PhoneButtonBase" TargetType="ButtonBase"> 
     <Setter Property="Background" Value="Transparent"/> 
     <Setter Property="BorderBrush" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="Foreground" Value="{StaticResource PhoneForegroundBrush}"/> 
     <Setter Property="BorderThickness" Value="{StaticResource PhoneBorderThickness}"/> 
     <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilySemiBold}"/> 
     <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/> 
     <Setter Property="Padding" Value="10,5,10,6"/> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="ButtonBase"> 
        <Grid Background="Transparent"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
           <VisualState x:Name="Pressed"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneButtonBasePressedForegroundBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneAccentBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="BorderBrush" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Background" Storyboard.TargetName="ButtonBackground"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="Transparent"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <Border x:Name="ButtonBackground" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" CornerRadius="0" Margin="{StaticResource PhoneTouchTargetOverhang}"> 
          <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
         </Border> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
    <Style x:Key="PhoneRadioButtonCheckBoxBase" BasedOn="{StaticResource PhoneButtonBase}" TargetType="ToggleButton"> 
     <Setter Property="Background" Value="{StaticResource PhoneRadioCheckBoxBrush}"/> 
     <Setter Property="BorderBrush" Value="{StaticResource PhoneRadioCheckBoxBorderBrush}"/> 
     <Setter Property="FontSize" Value="{StaticResource PhoneFontSizeMedium}"/> 
     <Setter Property="FontFamily" Value="{StaticResource PhoneFontFamilyNormal}"/> 
     <Setter Property="HorizontalContentAlignment" Value="Left"/> 
     <Setter Property="VerticalContentAlignment" Value="Center"/> 
     <Setter Property="Padding" Value="0"/> 
    </Style> 
    <Style x:Key="RadioButtonStyle1" BasedOn="{StaticResource PhoneRadioButtonCheckBoxBase}" TargetType="RadioButton"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="RadioButton"> 
        <Grid Background="Transparent"> 
         <VisualStateManager.VisualStateGroups> 
          <VisualStateGroup x:Name="CommonStates"> 
           <VisualState x:Name="Normal"/> 
           <VisualState x:Name="MouseOver"/> 
           <VisualState x:Name="Pressed"/> 
           <VisualState x:Name="Disabled"> 
            <Storyboard> 
             <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Foreground" Storyboard.TargetName="ContentContainer"> 
              <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PhoneDisabledBrush}"/> 
             </ObjectAnimationUsingKeyFrames> 
            </Storyboard> 
           </VisualState> 
          </VisualStateGroup> 
          <VisualStateGroup x:Name="CheckStates"> 
           <VisualState x:Name="Checked"/> 
           <VisualState x:Name="Unchecked"/> 
           <VisualState x:Name="Indeterminate"/> 
          </VisualStateGroup> 
         </VisualStateManager.VisualStateGroups> 
         <ContentControl x:Name="ContentContainer" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Foreground="{TemplateBinding Foreground}" FontSize="{TemplateBinding FontSize}" FontFamily="{TemplateBinding FontFamily}" HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}" Padding="{TemplateBinding Padding}" VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"/> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 

</phone:PhoneApplicationPage.Resources> 

.. 

<phone:LongListSelector x:Name="Recent" Margin="0,0,0,72" 
            LayoutMode="Grid" GridCellSize="108,108" 
            SelectionChanged="recent_SelectionChanged"> 

       <phone:LongListSelector.ItemTemplate> 
        <DataTemplate> 
         <ContentControl HorizontalAlignment="Stretch" HorizontalContentAlignment="Left"> 
          <ContentControl.Resources> 
           <Storyboard x:Name="CheckedStoryboard"> 
            <ColorAnimation Duration="0" To="#FF1BA1E2" Storyboard.TargetProperty="(Border.BorderBrush).(SolidColorBrush.Color)" Storyboard.TargetName="brd" d:IsOptimized="True"/> 
           </Storyboard> 
          </ContentControl.Resources> 
          <RadioButton x:Name="radioButton" HorizontalAlignment="Stretch" Margin="0,0,0,0" GroupName="A" Background="Black" Style="{StaticResource RadioButtonStyle1}" > 
           <i:Interaction.Triggers> 
            <i:EventTrigger EventName="Click"> 
             <eim:ControlStoryboardAction Storyboard="{StaticResource CheckedStoryboard}"/> 
            </i:EventTrigger> 
            <i:EventTrigger EventName="Unchecked"> 
             <eim:ControlStoryboardAction ControlStoryboardOption="Stop" Storyboard="{StaticResource CheckedStoryboard}"/> 
            </i:EventTrigger> 
           </i:Interaction.Triggers> 
           <Border x:Name="MyBorder" Background="Transparent"> 
            <Border x:Name="brd" CornerRadius="10" Width="Auto" BorderThickness="3" BorderBrush="Transparent"> 
             <toolkit:ContextMenuService.ContextMenu> 
              <toolkit:ContextMenu x:Name="imgListContextMenu" Background="{StaticResource PhoneChromeBrush}">                          <toolkit:MenuItem Foreground="{StaticResource PhoneForegroundBrush}" Header="{Binding Path=LocalizedResources.MainPage_ContextMenu_Delete, Source={StaticResource LocalizedStrings}}" Click="deleteContextMenuItem_Click"/> 
              </toolkit:ContextMenu> 
             </toolkit:ContextMenuService.ContextMenu> 
             <Viewbox Width="108" Height="108"> 
              <Image x:Name="recentImage" Source="{Binding Source}" Margin="6,6" Width="108"/> 
             </Viewbox> 
            </Border> 
           </Border> 
          </RadioButton> 
         </ContentControl> 
        </DataTemplate> 
       </phone:LongListSelector.ItemTemplate> 

      </phone:LongListSelector> 

MainPage.xaml.cs

protected override void OnNavigatedFrom(NavigationEventArgs e) 
    { 
     base.OnNavigatedFrom(e); 

     //if a LongListSElector item is currently selected, unselect it and remove border     
    } 

private void recent_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
     var item = (sender as LongListSelector).SelectedItem; 
     if (item == null) 
      return; 

     capturedPicture = null; 
     //Get picture 
     capturedPicture = (sender as LongListSelector).SelectedItem as CapturedPicture; 

     if (capturedPicture != null) 
     {     
      currentPhoto.DataContext = null; 
      fileName = null; 
      currentPhoto.DataContext = capturedPicture; 
      fileName = capturedPicture.FileName; 
     } 
    } 
+0

Можете ли вы мне помочь на этом http://stackoverflow.com/questions/22706930/long-list-selector-with-item-tap-animation – user2056563

ответ

0

Чтобы отменить выбор, просто установите SelectedItem свойство нуль:

Recent.SelectedItem = null; 
+0

Спасибо, это действительно очищает выбранный элемент, хотя граница все еще существует? Как я могу отключить границу? – Matthew

+0

Не проверял еще на моей машине dev (не сейчас), но я всегда использую .SelectedIndex = -1 – Depechie

+0

Хм, мне действительно интересно узнать, удаляет ли это границу. Единственный другой вариант, о котором я могу думать, - это контролировать StoryBoard в коде позади, с которым у меня нет опыта. – Matthew

Смежные вопросы