【问题标题】:Xamarin How to bind a class property (from another project) to the Picker.ItemDisplayBinding propertyXamarin 如何将类属性(来自另一个项目)绑定到 Picker.ItemDisplayBinding 属性
【发布时间】:2021-12-14 08:24:18
【问题描述】:

我正在尝试在选择器中填充和显示 List"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"),但没有成功

感谢您的帮助!

【问题讨论】:

  • magasin 类的外观如何?
  • 您好,刚刚编辑了我的代码以添加 magasin 类!

标签: c# xamarin binding dependencies picker


【解决方案1】:

我认为这是因为 Name 和 Code 只是字段,它们应该是一个属性。尝试改变 magasin 类,如

public string Name {get;set;} 
public string Code {get;set;} 

那么Binding("Name") 就可以了。

【讨论】:

  • 这就是问题所在!非常感谢:)
  • 为了“清楚”,我的问题只需要 Name 属性上的 {get;set;} !但再次感谢:)
猜你喜欢
  • 2023-04-08
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 1970-01-01
  • 2018-04-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
相关资源
最近更新 更多