基于布尔字段填充listView



我试图根据列表中每个项目的布尔字段填充列表视图。我无法理解我应该如何实施此操作。因此,基本上,每次我单击添加 delete 时,都会更新用户列表。但是,我要做的是 - 一旦用户单击 delete 并在用户单击添加时设置为true。并基于此,用户将仅看到将其值设置为true的列表中的项目。因此,简而言之,该程序的输出将与我当前的程序相同。除了用户单击 delete 时,他们实际上不会从列表中删除元素,但仍将继续在UI中看到该项目。这是我的示例XAML,代码和模型。

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>

有解决方案。 通过更改修改了问题中的代码。编辑说明包含更新。

相关内容

  • 没有找到相关文章

最新更新