C# 包含选择器的自定义控件中的Binding ItemSource属性
我正在尝试创建一个自定义控件,其中包含一个带有Xamarin.Forms的选择器。 问题是,当尝试绑定ItemSource属性时,它永远不会被绑定,当我触摸移动设备上的自定义控件时,它会显示一个没有绑定项的空对话框 注意:我尝试了在“Stack OverFlow”或“forums.xamarin”上找到的几乎所有解决方案,但没有一个对我有效 这是我的密码: 对于自定义控件XAML文件(以“HitPicker”命名):C# 包含选择器的自定义控件中的Binding ItemSource属性,c#,xamarin,xamarin.forms,wpf-controls,C#,Xamarin,Xamarin.forms,Wpf Controls,我正在尝试创建一个自定义控件,其中包含一个带有Xamarin.Forms的选择器。 问题是,当尝试绑定ItemSource属性时,它永远不会被绑定,当我触摸移动设备上的自定义控件时,它会显示一个没有绑定项的空对话框 注意:我尝试了在“Stack OverFlow”或“forums.xamarin”上找到的几乎所有解决方案,但没有一个对我有效 这是我的密码: 对于自定义控件XAML文件(以“HitPicker”命名): 对于自定义控件cs文件: public static readonly B
对于自定义控件cs文件:
public static readonly BindableProperty ItemsSourceProperty = BindableProperty.Create("ItemsSource", typeof(List<string>), typeof(HitPicker), default(List<string>), BindingMode.TwoWay, null, OnItemsSourceChanged);
public List<string> ItemsSource
{
get => (List<string>)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}
public HitPicker()
{
InitializeComponent();
BindingContext = this;
}
private static void OnItemsSourceChanged(BindableObject bindable, object oldvalue, object newvalue)
{
var picker = (bindable as HitPicker).PickerField;
picker.Items.Clear();
var newList = newvalue as List<string>;
if (newvalue != null)
{
foreach (var item in newList)
{
picker.Items.Add(item.ToString());
}
}
}
public static readonly BindableProperty ItemsSourceProperty=BindableProperty.Create(“ItemsSource”、typeof(List)、typeof(HitPicker)、default(List)、BindingMode.TwoWay、null、OnItemSourceChanged);
公共列表项资源
{
get=>(列表)GetValue(ItemsSourceProperty);
set=>SetValue(ItemsSourceProperty,value);
}
公共挑选者()
{
初始化组件();
BindingContext=这个;
}
私有静态资源已更改(BindableObject bindable、object oldvalue、object newvalue)
{
var picker=(可绑定为HitPicker);
picker.Items.Clear();
var newList=newvalue作为列表;
if(newvalue!=null)
{
foreach(新列表中的变量项)
{
picker.Items.Add(item.ToString());
}
}
}
知道从未调用过OnItemSourceChanged方法,并且几乎所有与我类似的问题都会得到类似的答案,这就建议将此方法放在控制类中
对于使用此控件的XAML文件:
<controls:HitPicker ItemsSource="{Binding MonkeyList}" Title="Select monky" BackgroundColor="Azure"></controls:HitPicker>
下面是ViewModel中针对上述XAML的猴子列表声明:
private List<string> _lst = new List<string>{
"Baboon",
"Capuchin Monkey",
"Blue Monkey",
"Squirrel Monkey",
"Golden Lion Tamarin",
"Howler Monkey",
"Japanese Macaque"
};
public List<string> MonkeyList
{
get => _lst;
set
{
_lst = value;
OnPropertyChanged();
}
}
private List _lst=新列表{
“狒狒”,
“卷尾猴”,
“蓝猴”,
“松鼠猴”,
“金狮罗望子”,
“吼猴”,
“日本猕猴”
};
公共列表MonkeyList
{
get=>lst;
设置
{
_lst=值;
OnPropertyChanged();
}
}
MonkeyList getter永远不会被调用,因为在CustomControl中设置bindingcontext时,您知道绑定上下文是ViewModel
public HitPicker()
{
InitializeComponent();
BindingContext = this;
}
它将破坏自定义控件和ContentPage之间的绑定
所以您可以像下面这样修改代码
在HitPicker.xaml中
非常感谢,您可以为类似的问题提供参考,或者您从哪里知道设置BindingContext会破坏绑定。如果您可以编辑您的答案,告诉您删除OnItemSourceChanged方法,因为如果设置了ItemSource属性,则在修改选择器项时会抛出exeption。您只需直接删除它。当我们将ItemSource设置为可绑定属性时,在ViewModel中设置它时,它将自动更改(所以我们不需要手动添加)。我的评论分为两部分。对不起,你可能误解了我。你只需要直接删除OnItemSourceChanged。第一部分,你可以检查。在博客中,演示代码不使用自定义控件中的数据绑定(直接在代码隐藏中设置值)。因此,如果确实要使用数据绑定,则需要像上面的代码一样直接设置绑定路径。
public HitPicker()
{
InitializeComponent();
BindingContext = this;
}
<ContentView xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
//...
x:Name="pickerView" //set name of page >
<Picker x:Name="PickerField"
HeightRequest="46"
TitleColor="{Binding Source={x:Reference pickerView}, Path=TitleColor}"
TextColor="{Binding Source={x:Reference pickerView}, Path=TextColor}"
BackgroundColor="{Binding Source={x:Reference pickerView}, Path=BackgroundColor}"
Unfocused="Handle_Unfocused"
Focused="Handle_Focused"
SelectedItem="{Binding Source={x:Reference pickerView}, Path=SelectedItem}">
</Picker>
public HitPicker()
{
InitializeComponent();
//BindingContext = this;
}