I want to create a ListView where the ListViewItems have nested buttons next to the label, which only are shown on PointerOver. That's the DataTemplate I created for this. I tried it with VisualStateManager, but that doesn't work...
So the question is: What's the best pure-XAML way to do that? (or at least a working way would suffice ;) )
Thanks!
<DataTemplate x:Name="aUITodoListViewDataTemplate" x:DataType="a:aTodo">
<Grid HorizontalAlignment="Stretch" PointerEntered="Grid_PointerEntered" PointerExited="Grid_PointerExited">
<Grid.ColumnDefinitions>
<!-- creates a grid with three columns, the first so wide as needed, and the second two in a 7/1 ratio-->
<ColumnDefinition Width="auto" />
<ColumnDefinition Width="70*" MaxWidth="500" MinWidth="100"/>
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<!-- First is the actual label -->
<TextBlock Text="{x:Bind Name}" Grid.Column="0" VerticalAlignment="Center" />
<!-- the middle column is for spacing -->
<!-- the last column is for the buttons -->
<StackPanel Grid.Column="2" Orientation="Horizontal" x:Name="buttonGroup">
<Button>A</Button>
<Button>B</Button>
<Button>C</Button>
</StackPanel>
<!-- The visual states -->
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="buttonGroup" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Visible" />
<DiscreteObjectKeyFrame KeyTime="0:0:0.333" Value="Collapsed" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
<VisualState x:Name="PointerOver">
<Storyboard>
<ObjectAnimationUsingKeyFrames Storyboard.TargetName="buttonGroup" Storyboard.TargetProperty="Visibility">
<DiscreteObjectKeyFrame KeyTime="0" Value="Collapsed" />
<DiscreteObjectKeyFrame KeyTime="0:0:0.333" Value="Visible" />
</ObjectAnimationUsingKeyFrames>
</Storyboard>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
</DataTemplate>
You can make it work by providing a custom control template for
ListViewItem. For example:For context, my toy model object looks like this:
...and the page itself (named
self) exposes the following collection property:Note that you can't bind to your data model from within the
ControlTemplate, hence theDataTemplatewith aTextBlockbound toName, and theContentPresenterin theControlTemplate. Conversely, if you want to use the VSM from theDataTemplate, you need to set states programatically.Here is a related post.
The result is really ugly and looks as follows (with the mouse over "Two"):