c# - 在列表视图中显示隐藏图标
问题描述
假设我有一个包含 10 个 ListViewItems 的 listview,每个 ListViewItems 都有其他嵌套的 UIElements。每个 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>
解决方案
您可以在您的 Person 类中设置一个属性来绑定 AppBarButton 的 Visibility。当悬停在 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 的 Visibility。
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;
}
推荐阅读
- alexa - IR-Blasters 与 Alexa 智能家居的集成
- string - 从变量中提取某些字符串
- cloud - 无法 ssh 到通过 terraform 创建的 ibmcloud 实例
- adb - 使用 UIAutomator 在自动化脚本中将文件从本地系统推送到 android 设备
- java - 在 Select Query 中注入单个参数时,Prepared 语句参数出现问题
- javascript - 如何访问在 if-else 语句中分配的值?
- bash - 在 bash 中的特定行中搜索模式
- angular - 更新代码后的空白屏幕(Angular-NativeScript)
- mysql - 将 MySQL 结果格式化为货币
- kubernetes - kustomize:cert-manager 证书公用名中的命名空间(字符串内的变量)