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

C# 在列表视图中显示隐藏图标,c#,uwp,C#,Uwp,假设我有一个包含10个ListViewItems的listview,每个ListViewItems都有其他嵌套的UIElement。每个ListViewItem都有一个嵌套的AppBarButton。 默认情况下,在LisViewItem中AppBarButton可见性设置为折叠。我希望当用户将鼠标悬停在ListViewItem上时AppBarButton可见 ListViewItem附带了PointerEnter=“ListviewEnter”、PointerExit=“ListviewExi

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

ListViewItem附带了PointerEnter=“ListviewEnter”、PointerExit=“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>

您可以在Person类中设置属性以绑定AppBarButton的可见性。当鼠标悬停在ListViewItem上时,将属性设置为true以显示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;​
​
        }
人员类别:

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));​
        }​
    }
<AppBarButton  Visibility="{x:Bind IsShow,Mode=OneWay}"​ Icon="Delete" ​ Label="Delete" ​ HorizontalAlignment="Right"/>
XAML:

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));​
        }​
    }
<AppBarButton  Visibility="{x:Bind IsShow,Mode=OneWay}"​ Icon="Delete" ​ Label="Delete" ​ HorizontalAlignment="Right"/>

非常简单直观的解决方案。tnx解决了我的问题。@Faywang MSFT例如,它对我真的很有帮助。我正在寻找一种在listviewitem的datatemplates上实现悬停效果的方法,现在我找到了!非常感谢。