Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在MvvmCross+中选择Bindable Picker的EdItem不在Bindable stackLayout内工作;沙马林型_C#_Asp.net Mvc_Xamarin.forms - Fatal编程技术网

C# 在MvvmCross+中选择Bindable Picker的EdItem不在Bindable stackLayout内工作;沙马林型

C# 在MvvmCross+中选择Bindable Picker的EdItem不在Bindable stackLayout内工作;沙马林型,c#,asp.net-mvc,xamarin.forms,C#,Asp.net Mvc,Xamarin.forms,视图模型: List<PictureModel> _pages; public List<PictureModel> Pages { get { return _pages; } set { _pages = value; RaisePropertyChanged(() => Pages);

视图模型:

    List<PictureModel> _pages;
    public List<PictureModel> Pages
    {
        get
        {
            return _pages;
        }
        set
        {
            _pages = value;
            RaisePropertyChanged(() => Pages);
            CurrentPage = Pages.FirstOrDefault();
        }
    }

    private AttributeValue _selectedAttributeValue;

    public AttributeValue SelectedAttributeValue
    {
        get { return _selectedAttributeValue; }
        set
        {
            _selectedAttributeValue = value;
            RaisePropertyChanged(() => SelectedAttributeValue);
            SelectedAttributeCommand.Execute(null);

        }
    }

    public ICommand SelectedAttributeCommand
    {
        get
        {
            //return new MvxCommand(() => ShowViewModel<ReviewsPageViewModel>());
            return new MvxCommand(() =>
            {
                ChangePresentation(new MvxClosePresentationHint(new ProductListPageViewModel()));
            });

        }
    }
<controls:AwesomeWrappanel HorizontalOptions="CenterAndExpand"        Orientation="Horizontal" ItemsSource="{Binding ProductDeatil.ProductAttributes}" Spacing="10">
        <controls:AwesomeWrappanel.ItemTemplate>
                <DataTemplate>
                       <controls:BindablePicker ItemsSource="{Binding Values}" 
                         DisplayProperty="Name" Title="{Binding Name}" 
                         SelectedIndex="0" SelectedItem="{Binding 
                         SelectedAttributeValue, Mode=TwoWay}" />           
               </DataTemplate>
        </controls:AwesomeWrappanel.ItemTemplate>
</controls:AwesomeWrappanel>
public class BindablePicker : Picker
{
    public BindablePicker()
    {
        this.SelectedIndexChanged += OnSelectedIndexChanged;
    }


    public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create("SelectedItem", typeof(object), typeof(BindablePicker), null, BindingMode.TwoWay, null, new BindableProperty.BindingPropertyChangedDelegate(BindablePicker.OnSelectedItemChanged), null, null, null);
    public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(BindablePicker), null, BindingMode.TwoWay, null, new BindableProperty.BindingPropertyChangedDelegate(BindablePicker.OnItemsSourceChanged), null, null, null);
    public static readonly BindableProperty DisplayPropertyProperty = BindableProperty.Create("DisplayProperty", typeof(string), typeof(BindablePicker), null, BindingMode.TwoWay, null, new BindableProperty.BindingPropertyChangedDelegate(BindablePicker.OnDisplayPropertyChanged), null, null, null);

    public IList ItemsSource
    {
        get { return (IList)base.GetValue(BindablePicker.ItemsSourceProperty); }
        set { base.SetValue(BindablePicker.ItemsSourceProperty, value); }
    }

    public object SelectedItem
    {
        get { return base.GetValue(BindablePicker.SelectedItemProperty); }
        set
        {
            base.SetValue(BindablePicker.SelectedItemProperty, value);
            if (ItemsSource.Contains(SelectedItem))
            {
                SelectedIndex = ItemsSource.IndexOf(SelectedItem);
            }
            else
            {
                SelectedIndex = -1;
            }
        }
    }

    public string DisplayProperty
    {
        get { return (string)base.GetValue(BindablePicker.DisplayPropertyProperty); }
        set { base.SetValue(BindablePicker.DisplayPropertyProperty, value); }
    }

    private void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        this.SelectedItem = ItemsSource[SelectedIndex];
    }


    private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
    {
        BindablePicker picker = (BindablePicker)bindable;
        picker.SelectedItem = newValue;
        if (picker.ItemsSource != null && picker.SelectedItem != null)
        {
            int count = 0;
            foreach (object obj in picker.ItemsSource)
            {
                if (obj == picker.SelectedItem)
                {
                    picker.SelectedIndex = count;
                    break;
                }
                count++;
            }
        }
    }

    private static void OnDisplayPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        BindablePicker picker = (BindablePicker)bindable;
        picker.DisplayProperty = (string)newValue;
        loadItemsAndSetSelected(bindable);

    }
    private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var picker = (BindablePicker)bindable;
        picker.ItemsSource = (IList)newValue;

        var oc = newValue as INotifyCollectionChanged;

        if (oc != null && !PickerLookup.ContainsKey(oc))
        {
            oc.CollectionChanged += Oc_CollectionChanged;
            PickerLookup.Add(oc, new WeakReference(picker));
        }

        loadItemsAndSetSelected(bindable);
    }

    private static readonly Dictionary<INotifyCollectionChanged, WeakReference> PickerLookup = new Dictionary<INotifyCollectionChanged, WeakReference>();

    private static void Oc_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        var oc = (INotifyCollectionChanged)sender;

        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            var picker = (BindablePicker)PickerLookup[oc].Target;

            foreach (var newItem in e.NewItems)
            {
                var value = string.Empty;
                if (picker.DisplayProperty != null)
                {
                    var prop = newItem.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayProperty, StringComparison.OrdinalIgnoreCase));

                    if (prop != null)
                        value = prop.GetValue(newItem).ToString();
                }
                else
                {
                    value = newItem.ToString();
                }

                picker.Items.Add(value);
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            var picker = (BindablePicker)PickerLookup[oc].Target;

            foreach (var newItem in e.OldItems)
            {
                var value = string.Empty;
                if (picker.DisplayProperty != null)
                {
                    var prop = newItem.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayProperty, StringComparison.OrdinalIgnoreCase));

                    if (prop != null)
                        value = prop.GetValue(newItem).ToString();
                }
                else
                {
                    value = newItem.ToString();
                }

                picker.Items.Remove(value);
            }
        }
    }

    static void loadItemsAndSetSelected(BindableObject bindable)
    {

        BindablePicker picker = (BindablePicker)bindable;
        if (picker.ItemsSource as IEnumerable != null)
        {
            int count = 0;
            foreach (object obj in (IEnumerable)picker.ItemsSource)
            {
                string value = string.Empty;
                if (picker.DisplayProperty != null)
                {
                    var prop = obj.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayProperty, StringComparison.OrdinalIgnoreCase));
                    if (prop != null)
                    {
                        value = prop.GetValue(obj).ToString();
                    }
                }
                else
                {
                    value = obj.ToString();
                }
                picker.Items.Add(value);
                if (picker.SelectedItem != null)
                {
                    if (picker.SelectedItem == obj)
                    {
                        picker.SelectedIndex = count;
                    }
                }
                count++;
            }
        }
    }
}
List\u页面;
公共列表页
{
得到
{
返回页面;
}
设置
{
_页数=价值;
RaisePropertyChanged(()=>页);
CurrentPage=Pages.FirstOrDefault();
}
}
私有属性值_选择属性值;
公共属性值已选择属性值
{
获取{return\u selectedAttributeValue;}
设置
{
_selectedAttributeValue=值;
RaisePropertyChanged(()=>SelectedAttributeValue);
SelectedAttributeCommand.Execute(空);
}
}
公用ICommand,然后选择AttributeCommand
{
得到
{
//返回新的MvxCommand(()=>ShowViewModel());
返回新的MvxCommand(()=>
{
ChangePresentation(新的MvxClosePresentationHint(新的ProductListPageViewModel());
});
}
}
我的XAML文件:

    List<PictureModel> _pages;
    public List<PictureModel> Pages
    {
        get
        {
            return _pages;
        }
        set
        {
            _pages = value;
            RaisePropertyChanged(() => Pages);
            CurrentPage = Pages.FirstOrDefault();
        }
    }

    private AttributeValue _selectedAttributeValue;

    public AttributeValue SelectedAttributeValue
    {
        get { return _selectedAttributeValue; }
        set
        {
            _selectedAttributeValue = value;
            RaisePropertyChanged(() => SelectedAttributeValue);
            SelectedAttributeCommand.Execute(null);

        }
    }

    public ICommand SelectedAttributeCommand
    {
        get
        {
            //return new MvxCommand(() => ShowViewModel<ReviewsPageViewModel>());
            return new MvxCommand(() =>
            {
                ChangePresentation(new MvxClosePresentationHint(new ProductListPageViewModel()));
            });

        }
    }
<controls:AwesomeWrappanel HorizontalOptions="CenterAndExpand"        Orientation="Horizontal" ItemsSource="{Binding ProductDeatil.ProductAttributes}" Spacing="10">
        <controls:AwesomeWrappanel.ItemTemplate>
                <DataTemplate>
                       <controls:BindablePicker ItemsSource="{Binding Values}" 
                         DisplayProperty="Name" Title="{Binding Name}" 
                         SelectedIndex="0" SelectedItem="{Binding 
                         SelectedAttributeValue, Mode=TwoWay}" />           
               </DataTemplate>
        </controls:AwesomeWrappanel.ItemTemplate>
</controls:AwesomeWrappanel>
public class BindablePicker : Picker
{
    public BindablePicker()
    {
        this.SelectedIndexChanged += OnSelectedIndexChanged;
    }


    public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create("SelectedItem", typeof(object), typeof(BindablePicker), null, BindingMode.TwoWay, null, new BindableProperty.BindingPropertyChangedDelegate(BindablePicker.OnSelectedItemChanged), null, null, null);
    public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(BindablePicker), null, BindingMode.TwoWay, null, new BindableProperty.BindingPropertyChangedDelegate(BindablePicker.OnItemsSourceChanged), null, null, null);
    public static readonly BindableProperty DisplayPropertyProperty = BindableProperty.Create("DisplayProperty", typeof(string), typeof(BindablePicker), null, BindingMode.TwoWay, null, new BindableProperty.BindingPropertyChangedDelegate(BindablePicker.OnDisplayPropertyChanged), null, null, null);

    public IList ItemsSource
    {
        get { return (IList)base.GetValue(BindablePicker.ItemsSourceProperty); }
        set { base.SetValue(BindablePicker.ItemsSourceProperty, value); }
    }

    public object SelectedItem
    {
        get { return base.GetValue(BindablePicker.SelectedItemProperty); }
        set
        {
            base.SetValue(BindablePicker.SelectedItemProperty, value);
            if (ItemsSource.Contains(SelectedItem))
            {
                SelectedIndex = ItemsSource.IndexOf(SelectedItem);
            }
            else
            {
                SelectedIndex = -1;
            }
        }
    }

    public string DisplayProperty
    {
        get { return (string)base.GetValue(BindablePicker.DisplayPropertyProperty); }
        set { base.SetValue(BindablePicker.DisplayPropertyProperty, value); }
    }

    private void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        this.SelectedItem = ItemsSource[SelectedIndex];
    }


    private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
    {
        BindablePicker picker = (BindablePicker)bindable;
        picker.SelectedItem = newValue;
        if (picker.ItemsSource != null && picker.SelectedItem != null)
        {
            int count = 0;
            foreach (object obj in picker.ItemsSource)
            {
                if (obj == picker.SelectedItem)
                {
                    picker.SelectedIndex = count;
                    break;
                }
                count++;
            }
        }
    }

    private static void OnDisplayPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        BindablePicker picker = (BindablePicker)bindable;
        picker.DisplayProperty = (string)newValue;
        loadItemsAndSetSelected(bindable);

    }
    private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var picker = (BindablePicker)bindable;
        picker.ItemsSource = (IList)newValue;

        var oc = newValue as INotifyCollectionChanged;

        if (oc != null && !PickerLookup.ContainsKey(oc))
        {
            oc.CollectionChanged += Oc_CollectionChanged;
            PickerLookup.Add(oc, new WeakReference(picker));
        }

        loadItemsAndSetSelected(bindable);
    }

    private static readonly Dictionary<INotifyCollectionChanged, WeakReference> PickerLookup = new Dictionary<INotifyCollectionChanged, WeakReference>();

    private static void Oc_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        var oc = (INotifyCollectionChanged)sender;

        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            var picker = (BindablePicker)PickerLookup[oc].Target;

            foreach (var newItem in e.NewItems)
            {
                var value = string.Empty;
                if (picker.DisplayProperty != null)
                {
                    var prop = newItem.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayProperty, StringComparison.OrdinalIgnoreCase));

                    if (prop != null)
                        value = prop.GetValue(newItem).ToString();
                }
                else
                {
                    value = newItem.ToString();
                }

                picker.Items.Add(value);
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            var picker = (BindablePicker)PickerLookup[oc].Target;

            foreach (var newItem in e.OldItems)
            {
                var value = string.Empty;
                if (picker.DisplayProperty != null)
                {
                    var prop = newItem.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayProperty, StringComparison.OrdinalIgnoreCase));

                    if (prop != null)
                        value = prop.GetValue(newItem).ToString();
                }
                else
                {
                    value = newItem.ToString();
                }

                picker.Items.Remove(value);
            }
        }
    }

    static void loadItemsAndSetSelected(BindableObject bindable)
    {

        BindablePicker picker = (BindablePicker)bindable;
        if (picker.ItemsSource as IEnumerable != null)
        {
            int count = 0;
            foreach (object obj in (IEnumerable)picker.ItemsSource)
            {
                string value = string.Empty;
                if (picker.DisplayProperty != null)
                {
                    var prop = obj.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayProperty, StringComparison.OrdinalIgnoreCase));
                    if (prop != null)
                    {
                        value = prop.GetValue(obj).ToString();
                    }
                }
                else
                {
                    value = obj.ToString();
                }
                picker.Items.Add(value);
                if (picker.SelectedItem != null)
                {
                    if (picker.SelectedItem == obj)
                    {
                        picker.SelectedIndex = count;
                    }
                }
                count++;
            }
        }
    }
}

BindablePicker:

    List<PictureModel> _pages;
    public List<PictureModel> Pages
    {
        get
        {
            return _pages;
        }
        set
        {
            _pages = value;
            RaisePropertyChanged(() => Pages);
            CurrentPage = Pages.FirstOrDefault();
        }
    }

    private AttributeValue _selectedAttributeValue;

    public AttributeValue SelectedAttributeValue
    {
        get { return _selectedAttributeValue; }
        set
        {
            _selectedAttributeValue = value;
            RaisePropertyChanged(() => SelectedAttributeValue);
            SelectedAttributeCommand.Execute(null);

        }
    }

    public ICommand SelectedAttributeCommand
    {
        get
        {
            //return new MvxCommand(() => ShowViewModel<ReviewsPageViewModel>());
            return new MvxCommand(() =>
            {
                ChangePresentation(new MvxClosePresentationHint(new ProductListPageViewModel()));
            });

        }
    }
<controls:AwesomeWrappanel HorizontalOptions="CenterAndExpand"        Orientation="Horizontal" ItemsSource="{Binding ProductDeatil.ProductAttributes}" Spacing="10">
        <controls:AwesomeWrappanel.ItemTemplate>
                <DataTemplate>
                       <controls:BindablePicker ItemsSource="{Binding Values}" 
                         DisplayProperty="Name" Title="{Binding Name}" 
                         SelectedIndex="0" SelectedItem="{Binding 
                         SelectedAttributeValue, Mode=TwoWay}" />           
               </DataTemplate>
        </controls:AwesomeWrappanel.ItemTemplate>
</controls:AwesomeWrappanel>
public class BindablePicker : Picker
{
    public BindablePicker()
    {
        this.SelectedIndexChanged += OnSelectedIndexChanged;
    }


    public static readonly BindableProperty SelectedItemProperty = BindableProperty.Create("SelectedItem", typeof(object), typeof(BindablePicker), null, BindingMode.TwoWay, null, new BindableProperty.BindingPropertyChangedDelegate(BindablePicker.OnSelectedItemChanged), null, null, null);
    public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(BindablePicker), null, BindingMode.TwoWay, null, new BindableProperty.BindingPropertyChangedDelegate(BindablePicker.OnItemsSourceChanged), null, null, null);
    public static readonly BindableProperty DisplayPropertyProperty = BindableProperty.Create("DisplayProperty", typeof(string), typeof(BindablePicker), null, BindingMode.TwoWay, null, new BindableProperty.BindingPropertyChangedDelegate(BindablePicker.OnDisplayPropertyChanged), null, null, null);

    public IList ItemsSource
    {
        get { return (IList)base.GetValue(BindablePicker.ItemsSourceProperty); }
        set { base.SetValue(BindablePicker.ItemsSourceProperty, value); }
    }

    public object SelectedItem
    {
        get { return base.GetValue(BindablePicker.SelectedItemProperty); }
        set
        {
            base.SetValue(BindablePicker.SelectedItemProperty, value);
            if (ItemsSource.Contains(SelectedItem))
            {
                SelectedIndex = ItemsSource.IndexOf(SelectedItem);
            }
            else
            {
                SelectedIndex = -1;
            }
        }
    }

    public string DisplayProperty
    {
        get { return (string)base.GetValue(BindablePicker.DisplayPropertyProperty); }
        set { base.SetValue(BindablePicker.DisplayPropertyProperty, value); }
    }

    private void OnSelectedIndexChanged(object sender, EventArgs e)
    {
        this.SelectedItem = ItemsSource[SelectedIndex];
    }


    private static void OnSelectedItemChanged(BindableObject bindable, object oldValue, object newValue)
    {
        BindablePicker picker = (BindablePicker)bindable;
        picker.SelectedItem = newValue;
        if (picker.ItemsSource != null && picker.SelectedItem != null)
        {
            int count = 0;
            foreach (object obj in picker.ItemsSource)
            {
                if (obj == picker.SelectedItem)
                {
                    picker.SelectedIndex = count;
                    break;
                }
                count++;
            }
        }
    }

    private static void OnDisplayPropertyChanged(BindableObject bindable, object oldValue, object newValue)
    {
        BindablePicker picker = (BindablePicker)bindable;
        picker.DisplayProperty = (string)newValue;
        loadItemsAndSetSelected(bindable);

    }
    private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
    {
        var picker = (BindablePicker)bindable;
        picker.ItemsSource = (IList)newValue;

        var oc = newValue as INotifyCollectionChanged;

        if (oc != null && !PickerLookup.ContainsKey(oc))
        {
            oc.CollectionChanged += Oc_CollectionChanged;
            PickerLookup.Add(oc, new WeakReference(picker));
        }

        loadItemsAndSetSelected(bindable);
    }

    private static readonly Dictionary<INotifyCollectionChanged, WeakReference> PickerLookup = new Dictionary<INotifyCollectionChanged, WeakReference>();

    private static void Oc_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    {
        var oc = (INotifyCollectionChanged)sender;

        if (e.Action == NotifyCollectionChangedAction.Add)
        {
            var picker = (BindablePicker)PickerLookup[oc].Target;

            foreach (var newItem in e.NewItems)
            {
                var value = string.Empty;
                if (picker.DisplayProperty != null)
                {
                    var prop = newItem.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayProperty, StringComparison.OrdinalIgnoreCase));

                    if (prop != null)
                        value = prop.GetValue(newItem).ToString();
                }
                else
                {
                    value = newItem.ToString();
                }

                picker.Items.Add(value);
            }
        }
        else if (e.Action == NotifyCollectionChangedAction.Remove)
        {
            var picker = (BindablePicker)PickerLookup[oc].Target;

            foreach (var newItem in e.OldItems)
            {
                var value = string.Empty;
                if (picker.DisplayProperty != null)
                {
                    var prop = newItem.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayProperty, StringComparison.OrdinalIgnoreCase));

                    if (prop != null)
                        value = prop.GetValue(newItem).ToString();
                }
                else
                {
                    value = newItem.ToString();
                }

                picker.Items.Remove(value);
            }
        }
    }

    static void loadItemsAndSetSelected(BindableObject bindable)
    {

        BindablePicker picker = (BindablePicker)bindable;
        if (picker.ItemsSource as IEnumerable != null)
        {
            int count = 0;
            foreach (object obj in (IEnumerable)picker.ItemsSource)
            {
                string value = string.Empty;
                if (picker.DisplayProperty != null)
                {
                    var prop = obj.GetType().GetRuntimeProperties().FirstOrDefault(p => string.Equals(p.Name, picker.DisplayProperty, StringComparison.OrdinalIgnoreCase));
                    if (prop != null)
                    {
                        value = prop.GetValue(obj).ToString();
                    }
                }
                else
                {
                    value = obj.ToString();
                }
                picker.Items.Add(value);
                if (picker.SelectedItem != null)
                {
                    if (picker.SelectedItem == obj)
                    {
                        picker.SelectedIndex = count;
                    }
                }
                count++;
            }
        }
    }
}
公共类BindablePicker:Picker
{
公共BindablePicker()
{
this.SelectedIndexChanged+=on SelectedIndexChanged;
}
public static readonly BindableProperty SelectedItemProperty=BindableProperty.Create(“SelectedItem”、typeof(object)、typeof(BindablePicker)、null、BindingMode.TwoWay、null、new BindableProperty.BindablePicker.OnSelectedItemChanged)、null、null;
public static readonly BindableProperty ItemsSourceProperty=BindableProperty.Create(“ItemsSource”,typeof(IEnumerable),typeof(BindablePicker),null,BindingMode.TwoWay,null,new BindableProperty.bindingPropertyChangedElegate(BindablePicker.onitemsourcechanged),null,null);
public static readonly BindableProperty DisplayProperty=BindableProperty.Create(“DisplayProperty”、typeof(string)、typeof(BindablePicker)、null、BindingMode.TwoWay、null、new BindableProperty.bindablepichangedelegate(BindablePicker.OnDisplayPropertyChanged)、null、null;
公共IList项目资源
{
获取{return(IList)base.GetValue(BindablePicker.ItemsSourceProperty);}
set{base.SetValue(BindablePicker.ItemsSourceProperty,value);}
}
公共对象SelectedItem
{
获取{return base.GetValue(BindablePicker.SelectedItemProperty);}
设置
{
base.SetValue(BindablePicker.SelectedItemProperty,值);
if(ItemsSource.Contains(SelectedItem))
{
SelectedIndex=ItemsSource.IndexOf(SelectedItem);
}
其他的
{
SelectedIndex=-1;
}
}
}
公共字符串显示属性
{
get{return(string)base.GetValue(BindablePicker.DisplayPropertyProperty);}
set{base.SetValue(BindablePicker.DisplayPropertyProperty,value);}
}
SelectedIndexChanged上的私有无效(对象发送方,事件参数e)
{
this.SelectedItem=ItemsSource[SelectedIndex];
}
SelectedItemChanged上的私有静态无效(BindableObject bindable、object oldValue、object newValue)
{
BindablePicker-picker=(BindablePicker)bindable;
picker.SelectedItem=newValue;
if(picker.ItemsSource!=null&&picker.SelectedItem!=null)
{
整数计数=0;
foreach(picker.ItemsSource中的对象obj)
{
if(obj==picker.SelectedItem)
{
picker.SelectedIndex=计数;
打破
}
计数++;
}
}
}
私有静态void OnDisplayPropertyChanged(BindableObject bindable、object oldValue、object newValue)
{
BindablePicker-picker=(BindablePicker)bindable;
picker.DisplayProperty=(字符串)newValue;
LoadItems和SetSelected(可绑定);
}
私有静态资源已更改(BindableObject bindable、object oldValue、object newValue)
{
var picker=(BindablePicker)bindable;
picker.ItemsSource=(IList)newValue;
var oc=INotifyCollectionChanged时的新值;
if(oc!=null&&!PickerLookup.ContainsKey(oc))
{
oc.CollectionChanged+=oc_CollectionChanged;
PickerLookup.Add(oc,新WeakReference(picker));
}
LoadItems和SetSelected(可绑定);
}
私有静态只读字典PickerLookup=new Dictionary();
私有静态无效Oc_CollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
var oc=(INotifyCollectionChanged)发送方;
if(e.Action==NotifyCollectionChangedAction.Add)
{
var picker=(BindablePicker)PickerLookup[oc].Target;
foreach(e.NewItems中的var newItem)
{
var值=string.Empty;
if(picker.DisplayProperty!=null)
{
var prop=newItem.GetType().GetRuntimeProperties().FirstOrDefault(p=>string.Equals(p.Name,picker.DisplayProperty,StringComparison.OrdinalIgnoreCase));
如果(prop!=null)
value=prop.GetValue(newItem).ToString();
}
其他的
{
value=newItem.ToString();
}
选择器。项目。添加(值);
}
}
else if(e.Action==NotifyCollectionChangedAction.Remove)
{
var picker=(BindablePicker)PickerLookup[oc].Target;
foreach(e.OldItems中的var newItem)
{
var值=string.Empty;
if(picker.DisplayProperty!=null)
{