自定义选择器C#UWP上的多项选择 [英] multiple selection on custom picker C# UWP

查看:152
本文介绍了自定义选择器C#UWP上的多项选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在论坛和Internet上寻找有关C#上的自定义选择器的信息,我已经有一个选择器,但是我的一些研究让我发现您只能选择自定义选择器中的一项,这是用于部署选择器的代码.

I've been looking on forums and internet about a custom picker on C#, I have a picker already functional, but some of my research throw me that you are only able to select 1 item of the custom Picker, this is the code im using to deploy the picker.

选择器

<custom:CustomPicker x:Name="pickerCategories" ItemsSource="{Binding listCategoriesName}" SelectedIndex="{Binding SelectedCategory}" SelectedIndexChanged="pickerCategories_SelectedIndexChanged" Grid.Column="1" BackgroundColor="White"/>

将项目来源作为来自数据库的对象列表提供给选择器 有没有办法能够选择自定义选择器的多个索引? 例如...

the item source are given to the picker as a list of object from a database is there a way to be able to select multiple index of the custom picker? for Example...

Picker pk = new Picker(); pk.SelectionMode=Multiple; 

推荐答案

根据您的要求,您可以在本机uwp项目中创建自定义的Picker渲染器.然后创建一个新的DataTemplate来显示本机控件中包含复选框的ComboBox项.

For you requirement, you could make a custom Picker renderer in the native uwp project. And then make a new DataTemplate for displaying ComboBox item that contain checkbox in the native control.

<DataTemplate x:Key="templateEmployee" >
    <StackPanel  Orientation="Horizontal">
        <CheckBox Content="{Binding Content,Mode=TwoWay}" IsChecked="{Binding IsCheck,Mode=TwoWay}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/>
    </StackPanel>
</DataTemplate>

对于CustomPickerRenderer,您应该将Forms Picker项源传递给本机控件(ComboBox).当组合框下拉菜单关闭时,您可以执行InvokeAction方法将Data发送到表单Picker.

For CustomPickerRenderer, you should pass the Forms Picker item source to native control(ComboBox). And when combobox drop down closed, you could execute InvokeAction method to send Data to the Forms Picker.

public class CustomPickerRenderer : PickerRenderer
{
    protected override void OnElementChanged(ElementChangedEventArgs<Picker> e)
    {
        base.OnElementChanged(e);
        Control.ItemsSource = e.NewElement.ItemsSource;
        Control.ItemTemplate = (Windows.UI.Xaml.DataTemplate)App.Current.Resources["templateEmployee"];
        Control.DropDownClosed += Control_DropDownClosed;

    }

    private void Control_DropDownClosed(object sender, object e)
    {
        var NewElement = Element as CustomPicker;
        var items = (sender as ComboBox).ItemsSource;
        NewElement.InvokeAction(items);
    }

}

用法

public MainPage()
{
    InitializeComponent();
    MyPicker.ItemsSource = new MainViewModel().itemSource;
    MyPicker.RegisterAction(IsCheckItems);
}
private List<Item> SelecItms = new List<Item>();
private void IsCheckItems(object data)
{
    var items = data as ObservableCollection<Item>;
    var str = new StringBuilder();
    foreach (var item in items)
    {
        if (item.IsCheck)
        {
            SelecItms.Add(item);
            str.AppendLine(item.Content);
        }
    }
    SeleitemLabel.Text = str.ToString();
}

我已经上传了代码示例.请检查.

And I have uploaded the code sample. Please check.

这篇关于自定义选择器C#UWP上的多项选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆