基于布尔字段填充Listview

时间:2018-04-03 20:53:19

标签: wpf listview inotifypropertychanged listviewitem

我试图根据列表中每个项目的布尔字段填充列表视图。我无法理解我应该如何实现这一点。因此,基本上,每次点击 添加 删除 时,用户列表都会更新。但是,我要做的是 - 不要从列表中删除用户,而是在用户点击 <后,将每个用户的标志设置为false( isRemoved ) em> delete ,并在用户点击 添加 时设置为true。并且基于此,用户将仅看到列表中的值将被设置为true的项目。因此,简而言之 - 程序的输出将与我当前的程序相同。除了当用户点击 删除 时,他们实际上不会从列表中删除该元素,但仍会继续在UI中看不到该项。这是我的示例XAML,Code Behind和Model。

CodeBehind:

public partial class MainWindow : Window
{
    private ObservableCollection<User>users = new ObservableCollection<User>();

    public MainWindow()
    {
        InitializeComponent();

        users.Add(new User() { Name = "John Smith" });
        users.Add(new User() { Name = "Brad Pitt" });

        lbUsers.ItemsSource = users;
    }

    private void btnAddUser_Click(object sender, RoutedEventArgs e)
    {
        users.Add(new User() { Name = "New user" });
    }

    private void btnChangeUser_Click(object sender, RoutedEventArgs e)
    {
        if (lbUsers.SelectedItem != null)
        {
            (lbUsers.SelectedItem as User).Name = "Random Name";

        }
    }

    private void btnDeleteUser_Click(object sender, RoutedEventArgs e)
    {
        if (lbUsers.SelectedItem != null)
        //users.Remove(lbUsers.SelectedItem as User);
        {
            User delted = lbUsers.SelectedItem as User;
            delted.IsRemoved = true;
        }
    }
}

型号:

public class User : INotifyPropertyChanged
{
    private string name;
    private bool isRemoved;
    public event PropertyChangedEventHandler PropertyChanged;
    public String Name
    {
        get { return this.name; }
        set
        {
            if (this.name != value)
            {
                this.name = value;
                this.NotifyPropertyChanged("Name");
            }
        }
    }

    public bool IsRemoved
    {
        get { return this.isRemoved; }
        set
        {
            if (this.isRemoved != value)
            {
                this.isRemoved = value;
                this.NotifyPropertyChanged("IsRemoved");
            }
        }
    }
    public void NotifyPropertyChanged(string propName)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
    }

}

XAML

  <DockPanel Margin="10">
        <StackPanel DockPanel.Dock="Right" Margin="10,0,0,0">
            <Button Name="btnAddUser" Click="btnAddUser_Click">Add user</Button>
            <Button Name="btnChangeUser" Click="btnChangeUser_Click" Margin="0,5">Change user</Button>
            <Button Name="btnDeleteUser" Click="btnDeleteUser_Click">Delete user</Button>
        </StackPanel>

    <Border BorderBrush="Gainsboro" BorderThickness="1">

        <ListView Name="lbUsers">
            <ListView.ItemContainerStyle>
                <Style TargetType="{x:Type ListViewItem}">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding IsRemoved}" Value="true">
                            <Setter Property="Visibility" Value="Collapsed"/>
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </ListView.ItemContainerStyle>
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Name}"   />
                    </DataTemplate>
                </ListView.ItemTemplate>


        </ListView>
    </Border>
</DockPanel>

1 个答案:

答案 0 :(得分:0)

让解决方案奏效。  使用更改修改了问题中的代码。编辑说明包含更新。