Xamarin表单中自定义选取器上的ItemSource绑定
我有一个自定义控件,它由一个标签和一个自定义选择器组成。我需要将这些值绑定到Xaml中的ItemSource属性。这是我到目前为止尝试过的,但是选择器中没有显示任何值。 2017年10月4日编辑:我已将一个示例项目上载到github,该项目具有自定义控件:Xamarin表单中自定义选取器上的ItemSource绑定,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有一个自定义控件,它由一个标签和一个自定义选择器组成。我需要将这些值绑定到Xaml中的ItemSource属性。这是我到目前为止尝试过的,但是选择器中没有显示任何值。 2017年10月4日编辑:我已将一个示例项目上载到github,该项目具有自定义控件: 这是CustomControl Xaml文件 <?xml version="1.0" encoding="UTF-8"?> <ContentView xmlns="http://xamarin.com/schemas/201
这是CustomControl Xaml文件
<?xml version="1.0" encoding="UTF-8"?>
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:WoolQ.Core.Controls;assembly=WoolQ.Core"
x:Class="WoolQ.Core.Controls.CustomLabelWithPicker">
<Grid x:Name="grid" VerticalOptions="Start">
<Grid.RowDefinitions>
<RowDefinition Height="35" />
<RowDefinition Height="45" />
</Grid.RowDefinitions>
<controls:RoundedFrame OutlineColor="Red" Grid.Row="0" Grid.Column="0" Grid.RowSpan="2" />
<Label x:Name="title" Text="" Grid.Row="0" Margin="5,5,5,0" Style="{StaticResource CustomConrtolLabelStyle}" />
<controls:BorderlessPicker x:Name="pickerValue" Title="_" Grid.Row="1" Style="{StaticResource CustomConrtolPickerStyle}" ItemsSource="{Binding ItemSource}">
</controls:BorderlessPicker>
</Grid>
在代码背后,我有:
public static readonly BindableProperty ItemSourceProperty =
BindableProperty.Create(nameof(ItemSource), typeof(List<string>), typeof(CustomLabelWithPicker), null);
public List<string> ItemSource
{
get
{
return (List<string>)GetValue(ItemSourceProperty);
}
set
{
SetValue(ItemSourceProperty, value);
}
}
protected override void OnPropertyChanged(string propertyName = null)
{
base.OnPropertyChanged(propertyName);
if (propertyName == nameof(ItemSource))
{
this.pickerValue.Items.Clear();
if (ItemSource != null)
{
foreach (var item in ItemSource)
{
this.pickerValue.Items.Add(item);
}
}
}
}
公共静态只读BindableProperty ItemSourceProperty=
创建(nameof(ItemSource)、typeof(List)、typeof(CustomLabelWithPicker)、null);
公共列表项源
{
得到
{
返回(列表)GetValue(ItemSourceProperty);
}
设置
{
SetValue(ItemSourceProperty,value);
}
}
受保护的重写无效OnPropertyChanged(字符串propertyName=null)
{
base.OnPropertyChanged(propertyName);
if(propertyName==nameof(ItemSource))
{
this.pickerValue.Items.Clear();
if(ItemSource!=null)
{
foreach(ItemSource中的变量项)
{
this.pickerValue.Items.Add(item);
}
}
}
}
最后,在我看来,我用这样的列表绑定Itemsource
<controls:CustomLabelWithPicker Grid.Row="1" Grid.Column="2"
LabelText="Bin Codes" ItemSource="{Binding BinCodes}"/>
但我看不出有任何价值观。感谢您的帮助在自定义选择器中尝试以下代码:
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(CustomPicker), null, BindingMode.TwoWay, null, OnItemsSourceChanged);
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
var picker = (CustomPicker)bindable;
picker.ItemsSource = (IList)newValue;
}
在自定义选择器中尝试以下代码:
public static readonly BindableProperty ItemsSourceProperty =
BindableProperty.Create("ItemsSource", typeof(IEnumerable), typeof(CustomPicker), null, BindingMode.TwoWay, null, OnItemsSourceChanged);
public IList ItemsSource
{
get { return (IList)GetValue(ItemsSourceProperty); }
set { SetValue(ItemsSourceProperty, value); }
}
private static void OnItemsSourceChanged(BindableObject bindable, object oldValue, object newValue)
{
var picker = (CustomPicker)bindable;
picker.ItemsSource = (IList)newValue;
}
无法解析picker@Ziyad Godil上的DisplayProperty(),而且我们有一个可绑定的picker。那么,我们需要所有这些代码吗?当itemsource更改时,代码中是否有OnPropertyChanged方法调用?当我们在Xaml中定义了绑定时,我们不需要明确地调用任何东西。将调试点放入OnPropertyChanged方法并检查,该值是否在itemsource属性中?无法解析DisplayProperty()在picker@Ziyad Godiland上,我们还有一个可绑定的picker。那么,我们需要所有这些代码吗?当itemsource更改时,代码中是否有OnPropertyChanged方法调用?当我们在Xaml中定义了绑定时,我们不需要明确地调用任何东西,将调试点放入OnPropertyChanged方法并检查,该值是否在ItemSource属性中获取?可能消费者视图模型中未设置CustomLabelWithPicker的BindingContext,这就是它无法找到ItemsSource绑定的原因可能消费者视图模型中未设置CustomLabelWithPicker的BindingContext,这就是它无法找到ItemsSource绑定的原因