为什么我们要选择折叠的WPF ListView项

时间:2019-01-04 18:12:05

标签: c# wpf listview collapse

我创建了一个列表视图,并在其中填充了一个可观察的集合。我折叠了一行。。但是它仍然略显可见,可以选择。是否可以将其完全删除?

代码如下:

主窗口xaml:

<Window x:Class="ListViewProblem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ListViewProblem"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:ViewModel />
    </Window.DataContext>
    <Grid>
        <ListView ItemsSource="{Binding Numbers}">
            <ListView.ItemTemplate >
                <DataTemplate>
                    <Border Visibility="{Binding Visibility}">
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock VerticalAlignment="Center" Text="{Binding Value}" />
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListView.ItemTemplate>
            <ListView.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel Orientation="Vertical" />
                </ItemsPanelTemplate>
            </ListView.ItemsPanel>

        </ListView>
    </Grid>
</Window>

视图模型:

 public class ViewModel :INotifyPropertyChanged
    {
        private ObservableCollection<NumberVisibility> numbers;
        public ObservableCollection<NumberVisibility> Numbers
        {
            get => numbers;
            set
            {
                numbers = value;
                OnPropertyChanged("Numbers");
            }
        }

        public ViewModel()
        {
            Numbers = new ObservableCollection<NumberVisibility>();
            Numbers.Add(new NumberVisibility() { Value=1, Visibility = System.Windows.Visibility.Visible});
            Numbers.Add(new NumberVisibility() { Value = 2, Visibility = System.Windows.Visibility.Visible });
            Numbers.Add(new NumberVisibility() { Value = 3, Visibility = System.Windows.Visibility.Collapsed });
            Numbers.Add(new NumberVisibility() { Value = 4, Visibility = System.Windows.Visibility.Visible });
            Numbers.Add(new NumberVisibility() { Value = 5, Visibility = System.Windows.Visibility.Visible });
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName));
        }

        protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            this.PropertyChanged?.Invoke(this, e);
        }
    }

NumberVisibility对象:

public class NumberVisibility
{
    public int Value { get; set; }
    public Visibility Visibility { get; set; }
}

请注意,第3行已折叠,但仍占据一些像素并且可以选择。可以纠正吗?

感谢您的帮助。

0 个答案:

没有答案