在列表视图中显示隐藏图标

时间:2019-08-13 18:16:03

标签: c# uwp

假设我有一个包含10个ListViewItems的listview,每个ListViewItems具有其他嵌套的UIElement。每个ListViewItem都有一个嵌套的AppBarButton。 默认情况下,AppBarButton可见性设置为在LisViewItem中折叠。我希望当用户将鼠标悬停在ListViewItem上时,AppBarButton可见。

ListViewItem附加了PointerEntered =“ ListviewEnter”,PointerExited =“ ListviewExit”事件处理程序。

<ListView ItemsSource="{x:Bind people}">   
           <ListView.ItemContainerStyle>
                <Style TargetType="ListViewItem">
                    <Setter Property="HorizontalContentAlignment" 
                      Value="Stretch"/>
                </Style>
            </ListView.ItemContainerStyle>

          <ListView.ItemTemplate>
                <DataTemplate x:DataType="local:Person">

                    <ListViewItem 
                        PointerEntered="ListviewEnter"
                        PointerExited="ListviewExit"
                        Background="LightBlue">
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="Auto"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Grid.Column="0" Text="{x:Bind name}"/>
                            <TextBlock Grid.Column="1" Text="{x:Bind age}"/>
                            <Border Grid.Column="2" 
                                    BorderBrush="Green" 
                                    BorderThickness="1">
                                <AppBarButton 
                                   x:Name="ssss"
                                    Visibility="Collapsed"
                                    Icon="Delete" 
                                               Label="Delete" 
                                               HorizontalAlignment="Right"/>
                            </Border>

                        </Grid>


                    </ListViewItem>

                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

1 个答案:

答案 0 :(得分:1)

您可以在Person类中设置一个属性以绑定AppBarButton的可见性。将鼠标悬停在ListViewItem上时,请将属性设置为true以显示AppBarButton。

人员班级:

public class Person : INotifyPropertyChanged
    {​
        public event PropertyChangedEventHandler PropertyChanged = delegate { };​
        public String name ...;​
        public String age ...;​
        private bool isShow = false;​
        public bool IsShow​
        {​
            get { return isShow; }​
            set​
            {​
                isShow = value;​
                this.OnPropertyChanged();​
            }​
        }​
        public void OnPropertyChanged([CallerMemberName] string propertyName = null)​
        {​
            // Raise the PropertyChanged event, passing the name of the property whose value has changed.​
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));​
        }​
    }

XAML:

<AppBarButton  Visibility="{x:Bind IsShow,Mode=OneWay}"​ Icon="Delete" ​ Label="Delete" ​ HorizontalAlignment="Right"/>

隐藏代码:

当触发ListViewEnter和ListViewExit事件时,可以从中获取Person类并更改IsShow属性以控制AppBarButton的可见性。

private void ListviewEnter(object sender, PointerRoutedEventArgs e)
        {​
            ListViewItem item = sender as ListViewItem;​
            Person p = item.DataContext as Person;​
            p.IsShow = true;​
        }​
​
        private void ListviewExit(object sender, PointerRoutedEventArgs e)​
        {​
            ListViewItem item = sender as ListViewItem;​
            Person p = item.DataContext as Person;​
            p.IsShow = false;​
​
        }