[英]Xamarin.Forms Picker`s ItemDisplayBinding doesn't change displayed text when property on object changes
[英]Xamarin How to bind a class property (from another project) to the Picker.ItemDisplayBinding property
我正在嘗試在選擇器中填充和顯示列表“magasin”。
magasin 類來自另一個項目,添加到我的主項目的依賴項中。
我能夠填充選擇器,但我沒有設法設置 Picker.ItemDisplayBinding = new Binding("otherProject.magasin.Name"); ,它只是為每個項目顯示 otherProject.magasin 。
這是我的代碼:
using otherProject;
public partial class fForm: ContentPage
{
public List<otherProject.magasin> listeMagasin;
public fForm()
{
BindingContext = this;
InitializeComponent();
listeMagasin = otherProject.magasin.chargerMagasins();
if (listeMagasin != null)
{
pickerMagasin.ItemsSource = listeMagasin;
pickerMagasin.ItemDisplayBinding = new
Binding("otherProject.magasin.Name");
}
}
}
還有我的 XAML
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="mainProject.fForm">
<ContentPage.Content>
<StackLayout HorizontalOptions="FillAndExpand">
<Picker x:Name="pickerMagasin"></Picker>
</StackLayout>
</ContentPage.Content>
和magasin類
namespace otherProject
{
public class magasin
{
public string Code;
public string Name;
public magasin(string code)
{
Code = code;
}
public static List<magasin> chargerMagasins(){
//Code here}
}
}
我嘗試了 Binding("Name") 或 Binding("magasin.Name"),但沒有成功
謝謝你的幫助 !
我認為這是因為 Name 和 Code 只是字段,它們應該是一個屬性。 嘗試改變 magasin 類
public string Name {get;set;}
public string Code {get;set;}
然后Binding("Name")
將起作用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.