如何使用复选框项创建列表视图,并在uwp中选中全部复选框

问题描述 投票:0回答:2

我想创建一个包含部门名称的ListViewListView包含部门名称的CheckBox。用户可以选中和取消选中部门,用户也可以点击选中全部复选框,用户可以选择所有部门。

xaml xamarin.forms uwp uwp-xaml xamarin.uwp
2个回答
1
投票

哪个列表视图你想要一个简单的列表视图与textcellimagecellits到你,在这里我发布listview的代码与imagecell,也细胞刷卡选项,只需添加Checkbox你想要的事件和应用逻辑。希望这对你有用!

  <AbsoluteLayout>

        <ListView x:Name="Demolist" BackgroundColor="White" ItemSelected="Demolist_ItemSelected">

            <ListView.ItemTemplate>
                <DataTemplate>

                    <ImageCell Height="30"
                                Text="{Binding deparment_name }"
                           Detail="{Binding department_description}"
                            ImageSource="ImageName.png">

                        <ImageCell.ContextActions>
                            <MenuItem x:Name="OnMore" Clicked="OnMore_Clicked" CommandParameter="{Binding .}"  Text="More" />
                            <MenuItem x:Name="OnDelete" Clicked="OnDelete_Clicked" CommandParameter="{Binding .}" Text="Delete" IsDestructive="True" />
                        </ImageCell.ContextActions>
                    </ImageCell>

                </DataTemplate>
            </ListView.ItemTemplate>

        </ListView>

</AbsoluteLayout>

0
投票

复选框不是XF框架中的控件,因此我认为您无法在Xamarin.form中的listview中添加复选框,但您可以使用不同的复选框来显示检查和取消选中状态。

<ContentPage
x:Class="test2.Page3"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:convert="clr-namespace:test2"
x:Name="ToDoPage">
<ContentPage.Resources>
    <convert:converter1 x:Key="converterbool" />
</ContentPage.Resources>
<ContentPage.Content>
    <StackLayout>
        <ListView x:Name="listview1" ItemsSource="{Binding todoList}">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="4*" />
                                <ColumnDefinition Width="*" />

                            </Grid.ColumnDefinitions>
                            <Label Text="{Binding ItemDescription}" VerticalOptions="Center" />
                            <Button
                                Grid.Column="1"
                                Command="{Binding Source={x:Reference ToDoPage}, Path=BindingContext.UpdateCheckBoxCommand}"
                                CommandParameter="{Binding Id}"
                                Opacity="0" />
                            <Image
                                Grid.Column="1"
                                HeightRequest="20"
                                HorizontalOptions="Center"
                                Source="{Binding IsDone, Converter={StaticResource converterbool}}"
                                VerticalOptions="Center" />

                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>

public class TodoItem:INotifyPropertyChanged
{
    private string _Id;
    public string Id
    {
        get { return _Id; }
        set
        {
            _Id = value;
            RaisePropertyChanged("Id");
        }
    }
    private string _ItemDescription;
    public string ItemDescription
    {
        get { return _ItemDescription; }
        set
        {
            _ItemDescription = value;
            RaisePropertyChanged("ItemDescription");
        }
    }
    private bool _IsDone;
    public bool IsDone
    {
        get { return _IsDone; }
        set
        {
            _IsDone = value;
            RaisePropertyChanged("IsDone");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}


 class ToDoViewModel:INotifyPropertyChanged
{
    public ObservableCollection<TodoItem> todoList { get; set; }
    public ICommand UpdateCheckBoxCommand { get; set; }

    public ToDoViewModel()
    {
        todoList = new ObservableCollection<TodoItem>()
        {
            new TodoItem(){ Id = "1", ItemDescription = "Task 1", IsDone = false},
        new TodoItem(){ Id = "2", ItemDescription = "Task 2", IsDone = false},
        new TodoItem(){ Id = "3", ItemDescription = "Task 3", IsDone = false},
        new TodoItem(){ Id = "4", ItemDescription = "Task 4", IsDone = false},
            new TodoItem(){ Id = "5", ItemDescription = "Task 5",IsDone=false }
        };
        UpdateCheckBoxCommand = new Command((Id) => UpdateCheckBox(Id.ToString()));
    }

    private void UpdateCheckBox(string id)
    {
        IEnumerable<TodoItem> items = todoList.Where(x=>x.Id==id);

        foreach(var item in items )
        {
            if (item.IsDone) item.IsDone = false;
            else item.IsDone = true;
        }


    }

    public event PropertyChangedEventHandler PropertyChanged;


    public void RaisePropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}


class converter1 : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool ischeck = (bool)value;
        if(ischeck==false)
        {
            return "uncheck.png";
        }
        else
        {
            return "check.png";
        }

    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}


[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class Page3 : ContentPage
{
    public Page3 ()
    {
        InitializeComponent ();
        this.BindingContext = new ToDoViewModel();
    }
}

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.