我的项目中有多个选择器,由于某种原因,只有一个选择器没有绑定(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
似乎是问题所在。
自从你的Picker
的ItemsSource
(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/