在我的UWP中,我正在尝试创建一个ListView,它的DataTemplate将包含一个TextBox和一个不可见的AppBarButton。我想要做的是,当TextBox获得焦点或单击项目时,AppBarButton将设置为Visible。所以我创建了一个ControlTemplate,它包含一个VisualStateManager和AppBarButton,并将它设置为我的ListView的DataTemplate。
ControlTemplate:
<ControlTemplate x:Key="TelephoneTemplate" TargetType="ListViewItem">
<StackPanel Orientation="Vertical" >
<AppBarButton x:Name="DeleteIcon" Icon="Cancel" HorizontalAlignment="Right" Foreground="{StaticResource TextGreyBrush}" Height="40" Width="40" Margin="0 0 -12 0" Command="{Binding ElementName=ClientOverviewView, Path=DataContext.ClientDetailsViewModel.RemoveTelephoneCommand}" CommandParameter="{Binding}">
<AppBarButton.RenderTransform>
<CompositeTransform ScaleX="0.8" ScaleY="0.8"/>
</AppBarButton.RenderTransform>
</AppBarButton>
<ContentPresenter Content="{TemplateBinding Content}" />
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="DeleteIcon" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="Pressed">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="DeleteIcon" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="SelectionStates">
<VisualState x:Name="Selected">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="DeleteIcon" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
<VisualStateGroup x:Name="FocusStates">
<VisualState x:Name="Focused">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="DeleteIcon" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Visible"/>
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</StackPanel>
</ControlTemplate>
ListView:
<ListView Grid.Row="2" ItemsSource="{Binding ClientDetailsViewModel.ClientDetailsModel.Telephones}" SelectionMode="None" Margin="-10 4 -10 0" IsItemClickEnabled="True" >
<ListView.ItemTemplate>
<DataTemplate>
<ListViewItem Template="{StaticResource TelephoneTemplate}">
<ListViewItem.Content>
<TextBox
PlaceholderText="Telefon (erforderlich)"
InputScope="TelephoneNumber"
BorderThickness="1"
BorderBrush="{StaticResource TextGreyBrush}"
Foreground="{StaticResource TextGreyBrush}"
Text="{Binding ContactText, Mode=TwoWay}" >
</TextBox>
</ListViewItem.Content>
</ListViewItem>
</DataTemplate>
</ListView.ItemTemplate>
VisualState“Normal”似乎有效,因为当我将其值设置为“Collapsed”时,AppBarButton是不可见的。但是当我点击ListViewItem时,AppBarButton的可见性仍然是不可见的。我试过许多可见的状态,但它不起作用。