xamarin.forms - Xamarin 选择器未绑定(bind)

标签 xamarin.forms data-binding picker

我的项目中有多个选择器,由于某种原因,只有一个选择器没有绑定(bind)。我已经搞乱了两天了,无论我是否更改为可观察集合或字符串列表,它仍然不起作用。

这是xaml

                <Picker x:Name="PickerMarket" Title="Market" ClassId="PickerMarket"
                    ItemsSource="{Binding TestList}"
                    ItemDisplayBinding="{Binding ShortDesc}"  
                    SelectedItem="{Binding ShortDesc}"
                    Grid.Row="0" Grid.Column="1" >
            </Picker>

这是 View 模型

class VamiMarketViewModel: INotifyPropertyChanged
{
    private List<Performance> _testList;
    public List<Performance> TestList
    {
        get { return _testList; }
        set
        {
            {
                _testList = value;
                NotifyPropertyChanged();
            }
        }
    }

    private string _shortDesc;
    public string ShortDesc
    {
        get { return _shortDesc; }
        set
        {
            {
                _shortDesc = value;
                NotifyPropertyChanged();
            }
        }
    }

    public class Performance
{
    public int PerformanceDailyTableId { get; set; }
    public DateTime Filedate { get; set; }
    public string Office { get; set; }
    public string Account { get; set; }

    public decimal TradeLevel { get; set; }
    public decimal BeginningEquity { get; set; }
    public decimal fxPL { get; set; }
    public decimal CashActivity { get; set; }
    public decimal CashActivityNonPl { get; set; }
    public decimal TBills { get; set; }
    public decimal OTEChange { get; set; }
    public decimal Realized { get; set; }
    public decimal Commission { get; set; }
    public decimal ClearingFees { get; set; }
    public decimal ExchangeFees { get; set; }
    public decimal NFAFees { get; set; }
    public decimal BrokerageFees { get; set; }
    public decimal TransactionFees { get; set; }
    public decimal NetPerformance { get; set; }
    public decimal EndingEquity { get; set; }
    public decimal DailyROR { get; set; }
    public decimal Vami { get; set; }
    public decimal ADMstmtNLVchange { get; set; }
    public decimal ManualAdjustment { get; set; }
    public decimal DoubleCheck { get; set; }

    public string AccountNumber { get; set; }
    public string Sector { get; set; }
    public string ShortDesc { get; set; }
}

在仅出于测试目的创建 View 模型时,我尝试像这样填充列表

            Performance p1 = new Performance();
        p1.ShortDesc = "user";
        TestList.Add(p1);

        Performance p2 = new Performance();
        p2.ShortDesc = "stephen";
        TestList.Add(p2);

最佳答案

从你的代码中我看到,SelectedItem似乎是问题所在。 自从你的PickerItemsSource (TestList 属性)的类型为 List<Performance>SelectedItem属性绑定(bind)到 Picker类型必须为 Performance 。但是,就您而言,您将其保留为 string而不是Performance

ItemDisplayBinding必须是 Performance 中任何属性的名称对象,在您的情况下很好,因为您有一个名为 ShortDesc 的字符串属性在你的Performance里面类(class)。

这就是我在您的代码中看到的问题。更改属性的类型ShortDesc如下所示并分配您收藏中的任何一项 TestList到它。您的代码将开始正常工作。

private Performance _shortDesc;
public Performance ShortDesc
{
   get { return _shortDesc; } 
   set
   {
       {
            _shortDesc = value;
            NotifyPropertyChanged();
       }
   }
}

Refer to the documentation here which explains a clear example for Binding objects to Picker.

我希望这会有所帮助。

关于xamarin.forms - Xamarin 选择器未绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60100067/

相关文章:

c# - 漏洞? System.ArgumentException : 'unable to figure out route for:

c# - Xamarin.Forms 的 native 嵌入 : not finding UIKit in global namespace,

c# - 如何使用 Xamarin.Forms 中的转换器将文本转换为颜色?

c# - 使用 XPath 绑定(bind)表达式时,是否可以取回 InnerXml 而不是 InnerText?

ios - iOS 7 中文本字段的弹出/模式选择器

xamarin - 在 Xamarin.Forms RelativeLayout 中使用常量和因子?

javascript - 了解 Angular 数据绑定(bind)

C# WPF 绑定(bind)集合到 ListView

ios - UIPickerView 中 UIVIew 的背景颜色设置不正确

wordpress - 自定义 Wordpress 颜色选择器