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# - 如何在 ComboBox TemplateSelector 中强制更新 WPF 多重绑定(bind)?

WPF:简单的文本框数据绑定(bind)

android - 如何在android中选择文件夹?

iphone - 分页不同页面宽度的UIScrollView

c# - Xamarin Forms MasterDetailPage 主页隐藏菜单按钮

android - 处理 xamarin.forms 上的 iOS 和 Android 差异

javascript - 有没有办法直接绑定(bind)到监视箭头的 "number"输入字段?

ios - SwiftUI 中的一排 Picker 轮,为什么拖动滚动区域与屏幕上的 Picker 控件偏移?

c# - Xamarin - 绑定(bind)到 ControlTemplate 中的静态类

xamarin - 如何修改 Xamarin.Forms 中的 WebView 以在设备上的浏览器中打开链接?