Xamarin.forms 如何在Xamarin窗体选取器控件中使用Select作为默认选项

Xamarin.forms 如何在Xamarin窗体选取器控件中使用Select作为默认选项,xamarin.forms,picker,Xamarin.forms,Picker,我有一个选择器控件,它是可选字段,用户可以根据需要将选择器的选定项设置为空 是否可以将Select作为xamarin表单选取器控件的itemsource中的附加选项 自定义选择器的代码: <controls:CustomPicker Grid.Row="1" Grid.Column="1" Margin="0,5,0,0"

我有一个选择器控件,它是可选字段,用户可以根据需要将选择器的选定项设置为空

是否可以将Select作为xamarin表单选取器控件的itemsource中的附加选项

自定义选择器的代码:

    <controls:CustomPicker
                        Grid.Row="1"
                        Grid.Column="1"
                        Margin="0,5,0,0"
                        Image="Downarrow"
                        ItemDisplayBinding="{Binding abb}"
                        ItemsSource="{Binding StateList}"
                        Placeholder="Select"
                        SelectedIndex="{Binding StateSelectedIndex}"
                        Style="{StaticResource Key=PickerHeight}" />

     StateSelectedIndex = -1;

StateSelectedIndex=-1;
我尝试将所选索引设置为-1。仅当在选择器控件中未选择任何内容时,此选项才起作用,但如果从选择器中选择了一个项目,则无法选择选项“Select”(选择)(消失)

我试着引用下面的url,但这没有帮助


非常感谢您的帮助。

解决方案1: 您应该设置
SelectedIndex

SelectedIndex="{Binding StateSelectedIndex,Mode=TwoWay}"
解决方案2:

您可以在ViewModel中绑定
SelectedItem
的值

public class YourViewModel: INotifyPropertyChanged
public event propertychangedventhandler PropertyChanged;
私有void NotifyPropertyChanged([CallerMemberName]字符串propertyName=”“)
{
PropertyChanged?.Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
公共ObservableCollection MyItems{get;set;}
私有字符串选择项;
公共字符串SelectItem
{
收到
{
返回选择项;
}
设置
{
if(值!=null)
{
选择项=值;
NotifyPropertyChanged(“SelectItem”);
int SelectIndex=MyItems.IndexOf(值);
//当用户更改“选择、执行您想要的操作”时,将调用此选项
}
}
}

您能将完整的代码发布到代码隐藏和CustomPicker中吗?
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    public ObservableCollection<string> MyItems { get; set; }

    private string selectItem;
    public string SelectItem
    {
        get
        {
            return selectItem;
        }

        set
        {
            if(value!=null)
            {
                selectItem = value;
                NotifyPropertyChanged("SelectItem");


                int SelectIndex = MyItems.IndexOf(value);

               // this will been invoked when user change the select , do some thing you want

            }
        }
    }