I create a list based on the template, in which the text and Button . Button is hidden by default. One problem remained: How to make the Button appear only in the line on which the focus hangs (On Focused Item). How can I bind the Button.Visibility = Visible property only to the element that received the focus, and on the rest it should be Collapsed . Here is the code:

 <ListView x:Name="FileList" VerticalAlignment="Top" SelectionChanged="FileList_SelectionChanged" Foreground="#FF00FFE8" IsItemClickEnabled="True" Grid.Row="3" IsDoubleTapEnabled="False" IsRightTapEnabled="False" Grid.ColumnSpan="2"> <ListView.ItemContainerStyle> <Style TargetType="ListViewItem"> <Setter Property="HorizontalAlignment" Value="Stretch"/> </Style> </ListView.ItemContainerStyle> <ListView.ItemTemplate> <DataTemplate> <Grid VerticalAlignment="Center" HorizontalAlignment="Stretch" Width="{Binding ElementName=FileList, Path=ActualWidth}"> <Grid.ColumnDefinitions> <ColumnDefinition Width="300*"/> <ColumnDefinition Width="100"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding}" Height="30" Width="300" VerticalAlignment="Center" HorizontalAlignment="Left" Grid.Column="0"/> <Button Click="PlayButton_Click" Content="Play" Height="30" Width="50" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.Column="2" Visibility="Collapsed"/> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> 
  • possible using triggers. See the Interactivity: EventTrigger example. It should be easy there - Vladimir Paliukhovich

2 answers 2

The easiest is probably this:

 <ListView ItemsSource="<что у вас там>" HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled"> <ListView.Resources> <BooleanToVisibilityConverter x:Key="B2V"/> </ListView.Resources> <ListView.ItemTemplate> <DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="300*"/> <ColumnDefinition Width="100"/> </Grid.ColumnDefinitions> <TextBlock Text="{Binding}" Height="30" Width="300" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Left" /> <Button Content="Play" Height="30" Width="50" Grid.Column="2" VerticalAlignment="Center" HorizontalAlignment="Center" Visibility="{Binding IsKeyboardFocusWithin, RelativeSource= {RelativeSource FindAncestor, AncestorType=ListViewItem}, Converter={StaticResource B2V}}"> </Button> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> 

(To make Item stretch the full width, you don't need Binding / style, but HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled" on the ListView itself.)

  • On uwp ivent trigger does not work like. Thanks for the answer! - Morgomirius
  • @Morgomirius: Please. Glad if it helped. - VladD

At a focus event, you can display a button using VisualStates in the item template.