我正在开发UWP Win10 VS2015应用程序,并在ListviewItem样式中使用VisualStates ...我在FocusStates中有动画/故事板,它工作正常,但问题是当我们点击Listview外面所以我们失去焦点然后动画结束了。
实际上我需要在Selected visualState上启动动画并在Unselected visualstate上结束动画。动画工作正常,但仅限于PointerOver,PointerPressed,PointerFocused,Unfocused等,但我需要在Selected和Unselected visualstates上。
当我点击ListviewItem时,Colorband展开到右边,当我点击另一个项目时,先前聚焦的ListviewItem的色带被折叠并且当前聚焦的色带被扩展..我已经完成了这个并且它工作正常FocusStates Visualstates(PointerFocus / Unfocus),但问题是当我甚至在Listview外面点击所以Colorband崩溃,因为它失去了焦点和Unfocus visualstate触发......但我需要选择/未选择的视觉状态,所以即使我们点击在Listview项目之外,在我点击另一个listview项目之前,它不会是LostFocus。 Plz帮助。
Colorband的Storyboard和所有Visualstates都在样式代码中。正如我上面所说,这段代码和动画与给定的样式代码一起工作正常,但是如果我删除了FocusStates ......它将无法在SelectionStates上运行......我需要在SelectionStates上使用它。
答案 0 :(得分:2)
listview自定义选定和未选定状态的样式
<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);
}
}
}
}
答案 1 :(得分:1)
如果已实现ISelectionInfo,似乎RemovedItems将为null。 在这种情况下,您应该将删除的项目保存在DeselectRange中,然后在SelectionChanged(在DeselectRange之后调用)中访问它。
如果您没有上面讨论的自定义视觉状态,那么您将使用VisualStateManager.GoToState(lvi,&#34; Normal&#34;,true)。这将从ListViewItem(lvi)中删除突出显示。
这似乎是ListView中的一个错误。单击ListView外部的另一个控件,然后在ListView中选择一个新条目后,它不会检查所选ListViewItem的选择状态。