如何解决具有数据绑定的默认选择器项
我正在尝试在 Xamarin.Forms 中创建一个下拉列表,允许用户选择他们选择的货币符号。我希望 CurrencyList 的第一个元素是默认选择的符号。
目前,默认情况下没有选择任何元素 - 即使我在 Page1.cs 文件中使用了 CurrencyPicker.Selectedindex = 1;
。
Page1.xaml:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:appproject.viewmodels"
x:Class="appproject.Views.Page1">
<ContentPage.BindingContext>
<local:Currencyviewmodel></local:Currencyviewmodel>
</ContentPage.BindingContext>
<Grid Margin="0,0">
Grid.ColumnDeFinitions>
<ColumnDeFinition Width="0.1*" />
<ColumnDeFinition Width="0.0*" />
<ColumnDeFinition Width="0.4*" />
<ColumnDeFinition Width="0.5*" />
</Grid.ColumnDeFinitions>
<Grid.RowDeFinitions>
<RowDeFinition Height="50" />
</Grid.RowDeFinitions>
<Picker ItemdisplayBinding="{Binding CurrencySymbol}" x:Name="CurrencyPicker" ItemsSource="{Binding CurrencyList}" Grid.Column="0" Grid.Row="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
<Entry Grid.Column="2" Grid.Row="0" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>
</Grid>
Page1.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace appproject.Views
{
class Page1{
CurrencyPicker.Selectedindex = 1;
}
}
货币.cs:
using System;
using System.Collections.Generic;
using System.Text;
namespace appproject.Models
{
public class Currency
{
public int CurrencyID { get; set; }
public string CurrencySymbol { get; set; }
}
}
Currencyviewmodels.cs:
using appproject.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
namespace appproject.viewmodels
{
class Currencyviewmodel
{
public IList<Currency> CurrencyList { get; set; }
public Currencyviewmodel()
{
try
{
CurrencyList = new ObservableCollection<Currency>
{
new Currency { CurrencyID = 1,CurrencySymbol = "£" },new Currency { CurrencyID = 2,CurrencySymbol = "$" },new Currency { CurrencyID = 3,CurrencySymbol = "$" }
};
}
catch (Exception)
{
}
}
}
}
解决方法
首先让你的picker xaml代码像这样
<Label Grid.Row="2" Grid.Column="0" Text="Currency Symbol" Margin="5,0" Style="
{StaticResource baseLabelBold}" />
<Picker Grid.Row="2" Grid.Column="2" Grid.ColumnSpan="3"
ItemDisplayBinding="{Binding CurrencySymbol}" x:Name="CurrencyPicker" ItemsSource="
{Binding CurrencyList}"
SelectedItem="{Binding SelectedCurrency,Mode=TwoWay}">
</Picker>
像这样在 View Model 上取两个属性 Currency list 和 SelectedCurrency
private ObservableCollection<CurrencyList> _currencylist;
public ObservableCollection<CurrencyList> CurrencyList
{
get { return _currencylist; }
set
{
_currencylist = value;
base.RaisePropertyChanged(nameof(CurrencyList));
}
}
private CurrencyList _SelectedCurrency;
public CurrencyList SelectedCurrency
{
get { return _SelectedCurrency; }
set
{
_SelectedCurrency = value;
base.RaisePropertyChanged(nameof(SelectedCurrency));
}
}
现在创建一个类似 CurrencyList_Completed() 的方法,在您的集合中插入数据并将所选货币作为默认值
protected async Task CurrencyList_Completed()
{
_currencylist = new ObservableCollection<CurrencyList>();
_currencylist.Add(new CurrencyList() { CurrencyID = 1,CurrencySymbol = "£" });
_currencylist.Add(new CurrencyList() { CurrencyID = 2,CurrencySymbol = "$" });
_currencylist.Add(new CurrencyList() { CurrencyID = 3,CurrencySymbol = "$" });
CurrencyList = _currencylist.ToObservableCollection();
SelectedCurrency = CurrencyList.Where(x => x.CurrencyID == 1).FirstOrDefault();
}
在初始化您的 ViewModel 时调用此方法
protected override async Task Initialize(NavigationParameters parameters)
{
if (parameters.GetNavigationMode() == NavigationMode.New)
{
CurrencyList_Completed();
}
}
现在您可以获得预期的输出。请参阅附加的视频 Video
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。