尽管将IsDeleteble属性设置为“ IsDeletable = true”,但我无法删除芯片

时间:2019-04-21 15:40:08

标签: c# wpf material-design-in-xaml

尽管将IsDeletable属性设置为IsDeletable = True,  我一直单击芯片删除图标,但是什么也没发生。 单击删除图标如何删除芯片?

mainwindow.xaml

    <StackPanel Orientation="Horizontal" x:Name="addchip">
        <Button Click="create_chip">delete_chip</Button>
        <materialDesign:Chip
  Content="Example Chip"
  ToolTip="This is an example chip" IsDeletable="True"/>
    </StackPanel>

mainwindow.xaml.cs

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void deletenotify(object sender, RoutedEventArgs e)
    {

        Debug.WriteLine("delete!");

    }

    private void create_chip(object sender, RoutedEventArgs e)
    {
        Chip a = new Chip();
        a.Content = "Example Chip";
        a.IsDeletable =true ;
        a.DeleteClick += deletenotify;
        addchip.Children.Add(a);
    }
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

输出:

enter image description here

Xaml:

<Window x:Class="TestChipDeleting.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"
        mc:Ignorable="d"
        Title="MainWindow" 
        Height="400" 
        Width="300"
        Loaded="MainWindow_OnLoaded">
    <Grid>
        <ItemsControl Name="UsersListBox">

        </ItemsControl>
    </Grid>
</Window>

C#:

using System.Windows;
using MaterialDesignThemes.Wpf;

namespace TestChipDeleting
{
    public partial class MainWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
        {
            BindUsers();
        }

        private void Chip_OnDeleteClick(object sender, RoutedEventArgs e)
        {
            var currentChip = (Chip) sender;
            UsersListBox.Items.Remove(currentChip);
        }

        private void BindUsers()
        {
            for (var i = 0; i < 10; i++)
            {
                var chip = new Chip {IsDeletable = true};
                chip.DeleteClick += Chip_OnDeleteClick;
                chip.Content = "Username" + i;
                chip.Icon = i;
                UsersListBox.Items.Add(chip);
            }
        }


    }
}