2016-03-14 18 views
0

我正在開發UWP Win10 VS2015 App,並在ListviewItem樣式中使用VisualStates ...我在FocusStates中有動畫/故事板並且工作正常,但是問題是當我們點擊Listview之外,所以我們失去了焦點,然後動畫結束。UWP - VisualStates SelectionStates「Selected and Unselected」在ListviewItem中不起作用

其實我需要在選定的visualState上開始動畫並在Unselected visualstate上結束動畫。動畫工作正常,但只在PointerOver,PointerPressed,PointerFocused,Unfocused等,但我需要它在選定和未選中的視覺狀態。

當我點擊ListviewItem的Colorband擴展到右側,當我點擊另一個項目時,以前關注的ListviewItem的Colorband被摺疊,當前聚焦的Colorband被擴展..我已經做到了這一點,它在FocusStates VisualStates上正常工作PointerFocus/Unfocus),但問題是,當我甚至在Listview之外點擊,因此Colorband摺疊,因爲它失去了焦點和Unfocus visualstate觸發了...但我需要選擇/未選中的視覺狀態,這樣即使當我們點擊Listview之外項目它不會LostFocus直到我點擊另一個列表視圖項目。 Plz的幫助。

Colorband的故事板和所有VisualState都在樣式代碼中。正如我上面告訴過的,這個代碼和動畫可以很好地與給定的樣式代碼一起工作,但是如果我刪除了FocusStates ...它不會在SelectionStates上工作...而且我需要它在SelectionStates上。

+0

選中此鏈接。 https://msdn.microsoft.com/en-us/library/windows/apps/mt299136.aspx。選定和未選定的視覺狀態組是錯誤的 – Archana

+0

你想要什麼動畫? –

+0

Hay @ GraceFeng-MSFT,我已經用屏幕截圖和更多細節更新了我的問題,最終我需要什麼。 我已經通過另一個問題發佈了這個,但在不同的過程中...所以看看這個和那個。提前致謝。 http://stackoverflow.com/questions/35933968/uwp-how-to-get-listviewitem-currently-clicked-and-previously-clicked/35937053?noredirect=1#comment59626228_35937053 –

回答

1

樣式列表視圖定製選中和未選中狀態

<Style x:Key="ListViewItemStyle1" TargetType="ListViewItem"> 
      <Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}"/> 
      <Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}"/> 
      <Setter Property="Background" Value="Transparent"/> 
      <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}"/> 
      <Setter Property="TabNavigation" Value="Local"/> 
      <Setter Property="IsHoldingEnabled" Value="True"/> 
      <Setter Property="Padding" Value="12,0,12,0"/> 
      <Setter Property="HorizontalContentAlignment" Value="Left"/> 
      <Setter Property="VerticalContentAlignment" Value="Center"/> 
      <Setter Property="MinWidth" Value="{ThemeResource ListViewItemMinWidth}"/> 
      <Setter Property="MinHeight" Value="{ThemeResource ListViewItemMinHeight}"/> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="ListViewItem"> 
         <Grid x:Name="ContentBorder" 
       Background="{TemplateBinding Background}" 
       BorderBrush="{TemplateBinding BorderBrush}" 
       BorderThickness="{TemplateBinding BorderThickness}"> 
          <VisualStateManager.VisualStateGroups> 
           <VisualStateGroup x:Name="CustomStates"> 
            <VisualState x:Name="Unselected"> 
             <Storyboard> 
              <DoubleAnimation Duration="0:0:0.3" From="60" To="1" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Storyboard.TargetName="colorBand" EnableDependentAnimation="True"> 
               <DoubleAnimation.EasingFunction> 
                <QuadraticEase EasingMode="EaseOut"/> 
               </DoubleAnimation.EasingFunction> 
              </DoubleAnimation> 
             </Storyboard> 
            </VisualState> 
            <VisualState x:Name="CustomSelected"> 
             <Storyboard> 
              <DoubleAnimation Duration="0:0:0.3" From="1" To="60" Storyboard.TargetProperty="(UIElement.RenderTransform).(CompositeTransform.ScaleX)" Storyboard.TargetName="colorBand" EnableDependentAnimation="True"> 
               <DoubleAnimation.EasingFunction> 
                <QuadraticEase EasingMode="EaseOut"/> 
               </DoubleAnimation.EasingFunction> 
              </DoubleAnimation> 
             </Storyboard> 
            </VisualState> 
           </VisualStateGroup> 
           <VisualStateGroup x:Name="CommonStates"> 
            <VisualState x:Name="Normal"> 
            </VisualState> 
            <VisualState x:Name="PointerOver"> 
            </VisualState> 
            <VisualState x:Name="Pressed"> 
            </VisualState> 
            <VisualState x:Name="Selected"> 
            </VisualState> 
            <VisualState x:Name="PressedSelected"> 
            </VisualState> 
           </VisualStateGroup> 

          </VisualStateManager.VisualStateGroups> 
          <Border x:Name="colorBand" 
            Background="Red"          

            HorizontalAlignment="Left" 
            Width="15" 
            Height="20" 
            RenderTransformOrigin="0.5,0.5"> 
           <Border.RenderTransform> 
            <CompositeTransform/> 
           </Border.RenderTransform> 
          </Border> 
          <!--<Rectangle x:Name="BorderBackground" 
        IsHitTestVisible="False" 
        Fill="{ThemeResource SystemControlHighlightListAccentLowBrush}" 
        Opacity="0" 
        Control.IsTemplateFocusTarget="True"/>--> 
          <Grid x:Name="ContentPresenterGrid" 
       Background="Transparent" 
       Margin="0,0,0,0"> 
           <Grid.RenderTransform> 
            <TranslateTransform x:Name="ContentPresenterTranslateTransform"/> 
           </Grid.RenderTransform> 
           <ContentPresenter x:Name="ContentPresenter" 
          ContentTransitions="{TemplateBinding ContentTransitions}" 
          ContentTemplate="{TemplateBinding ContentTemplate}" 
          Content="{TemplateBinding Content}" 
          HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
          VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
          Margin="{TemplateBinding Padding}"/> 
          </Grid> 
          <!-- The 'Xg' text simulates the amount of space one line of text will occupy. 
          In the DataPlaceholder state, the Content is not loaded yet so we 
          approximate the size of the item using placeholder text. --> 

         </Grid> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style> 

代碼背後

private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 
      if (e.AddedItems != null) 
      { 
       foreach (var item in e.AddedItems) 
       { 
        Debug.WriteLine(item); 
        ListViewItem litem = (sender as ListView).ContainerFromItem(item) as ListViewItem; 
        if (litem != null) 
        { 
         VisualStateManager.GoToState(litem, "CustomSelected", true); 
        } 
       } 
      } 
      if (e.RemovedItems != null) 
      { 
       foreach (var item in e.RemovedItems) 
       { 
        ListViewItem litem = (sender as ListView).ContainerFromItem(item) as ListViewItem; 
        if (litem != null) 
        { 
         VisualStateManager.GoToState(litem, "Unselected", true); 
        } 
       } 
      } 

     } 
+0

你已經告訴RemovedItem列表爲空。我在Windows 10中檢查了它爲我工作。請發佈listview控制代碼來檢查問題 – Archana

+0

感謝您的興趣...我檢查過的樣式代碼不工作...您是否創建了用於演示的CustomStates或者這是visualstate的實際部分? 好吧,稍後我會爲RemovedItem分享SelectionChangedevent代碼。謝謝。 –

+0

我已經創建了它,它並不是listview視覺狀態的實際部分。只有當您更改 – Archana

1

似乎RemovedItems將是無效的,如果你已經實現ISelectionInfo。 在這種情況下,您應該將已移除的項目保存在DeselectRange中,然後在SelectionChanged(在DeselectRange之後調用)中訪問它。

如果您沒有上面討論的自定義視覺狀態,那麼您將使用VisualStateManager.GoToState(lvi,「Normal」,true)。這將從ListViewItem(lvi)中刪除高亮。

這似乎是ListView中的一個錯誤。在單擊ListView之外的另一個控件並在ListView中選擇新條目後,它不會檢查所選ListViewItem的選擇狀態。

相關問題