title author date CreateTime categories
WPF 鼠标移动到列表上 显示列表图标
lindexi
2018-08-10 19:16:51 +0800
2018-2-13 17:23:3 +0800
WPF

在列表新建一个图标,添加 Visibility

Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsMouseOver, Converter={StaticResource BooleanToVisibilityConverter}}"

我这里用的是 TextBlock ,鼠标移动就会显示

        <ListView AlternationCount="10">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="20"></ColumnDefinition>
                            <ColumnDefinition Width="Auto"></ColumnDefinition>
                            <ColumnDefinition ></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex),Mode=OneWay,Converter={StaticResource NumberAddOne}}"></TextBlock>
                        <TextBlock Grid.Column="1" Text="点击"
                                   Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=IsMouseOver, Converter={StaticResource BooleanToVisibilityConverter}}"></TextBlock>
                        <TextBlock Grid.Column="2" Text="lindexi"></TextBlock>
                    </Grid>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

WPF鼠标移动到列表上显示列表图标_C#

获取当前列表项,使用{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex),Mode=OneWay,Converter={StaticResource NumberAddOne}}。注意 AlternationCount 如果没设不会显示

        <ListView AlternationCount="10">
            <ListViewItem>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="20"></ColumnDefinition>
                        <ColumnDefinition ></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <TextBlock Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex),Mode=OneWay,Converter={StaticResource NumberAddOne}}"></TextBlock>
                    <TextBlock Grid.Column="1" Text="lindexi"></TextBlock>
                </Grid>
            </ListViewItem>
        </ListView>
    public class NumberAddOne : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var index = (int)value;
            return index + 1;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

为何添加上面转化,这个程序员和客户对于数组的开始是不同的。

WPF鼠标移动到列表上显示列表图标_C#_02