ListView项目上的删除按钮

时间:2015-12-07 15:23:02

标签: c# windows xaml listview uwp

我开始为UWP开发。我尝试创建一个填充ListView的{​​{1}}。现在我想在所有单个项目上创建一个按钮以删除它们。像Windows 10 Mail这样的东西:

我创造了:

enter image description here

x:bind

我想更改<ListView x:Name="listActivity" ItemsSource="{x:Bind list}" Grid.Column="0" Header="Activity List" HorizontalAlignment="Center" Width="auto" IsItemClickEnabled="True" ItemClick="showDel"> <ListView.ItemTemplate> <DataTemplate x:DataType="vm:Activity"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="3*"/> <ColumnDefinition Width="1*"/> </Grid.ColumnDefinitions> <TextBlock x:Name="txt" Text="{x:Bind name}" Grid.Column="0"/> <Button x:Name="delItem" Click="delActivity" Grid.Column="1" Visibility="Collapsed" Background="Transparent" Margin="100, 0, 0, 0"> <SymbolIcon Symbol="Delete"/> </Button> </Grid> </DataTemplate> </ListView.ItemTemplate> </ListView> visibility按钮,但是从C#我无法使用set name调用它。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

我建议使用绑定来执行此操作。

a)向名为Visible

的Activity类中添加一个新属性
public class Activity : INotifyPropertyChanged
{
    public string Name { get; set; }

    private bool _visible; 
    public bool Visible {
        get { return _visible; }
        set
        {
            _visible = value;
            RaisePropertyChanged("Visible");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}

b)声明一个新的类,将布尔值转换为可见性

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return value is Visibility && (Visibility)value == Visibility.Visible;
    }
}

c)最后更新你的绑定。不要忘记OneWay模式设置。

<Page.Resources>
    <local:BooleanToVisibilityConverter x:Key="VisibilityConverter"/>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <ListView ItemsSource="{x:Bind Activities}">
        <ListView.ItemTemplate>
            <DataTemplate x:DataType="local:Activity">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="3*"/>
                        <ColumnDefinition Width="1*"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock x:Name="txt" Text="{x:Bind Name}" Grid.Column="0"/>
                    <Button x:Name="delItem" Click="delItem_Click" Grid.Column="1" Visibility="{x:Bind Visible, Mode=OneWay, Converter={StaticResource VisibilityConverter}}" Background="Transparent" Margin="100, 0, 0, 0">
                        <SymbolIcon Symbol="Delete"/>
                    </Button>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
</Grid>

您现在可以直接通过代码访问“可见性”或您的控件。以下完整代码供参考。

using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace App1
{
public sealed partial class MainPage : Page
{ 
    public List<Activity> Activities { get; set; }

    public MainPage()
    {
        this.InitializeComponent();

        Activities = new List<Activity>();
        Activities.Add(new Activity { Name = "Activity1", Visible = true });
        Activities.Add(new Activity { Name = "Activity2", Visible = false });
    }

    private void delItem_Click(object sender, RoutedEventArgs e)
    {
        Activities.First().Visible = false;
    }
}

public class BooleanToVisibilityConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        return value is Visibility && (Visibility)value == Visibility.Visible;
    }
}

public class Activity : INotifyPropertyChanged
{
    public string Name { get; set; }

    private bool _visible; 
    public bool Visible {
        get { return _visible; }
        set
        {
            _visible = value;
            RaisePropertyChanged("Visible");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void RaisePropertyChanged(string name)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(name));
        }
    }
}
}