C# 具有数据绑定的默认选择器项
我试图在Xamarin.Forms中创建一个下拉列表,允许用户选择自己选择的货币符号。我希望CurrencyList的第一个元素是默认的选定符号 当前,默认情况下未选择任何元素-即使我使用的是C# 具有数据绑定的默认选择器项,c#,xamarin,xamarin.forms,C#,Xamarin,Xamarin.forms,我试图在Xamarin.Forms中创建一个下拉列表,允许用户选择自己选择的货币符号。我希望CurrencyList的第一个元素是默认的选定符号 当前,默认情况下未选择任何元素-即使我使用的是CurrencyPicker.SelectedIndex=1在Page1.cs文件中 Page1.xaml: <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="
CurrencyPicker.SelectedIndex=1代码>在Page1.cs文件中
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,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>
Currency.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)
{
}
}
}
}
使用appproject.Models;
使用制度;
使用System.Collections.Generic;
使用System.Collections.ObjectModel;
使用系统文本;
命名空间appproject.ViewModels
{
类CurrencyViewModel
{
公共IList CurrencyList{get;set;}
公共货币视图模型()
{
尝试
{
CurrencyList=新的ObservableCollection
{
新货币{CurrencyID=1,CurrencySymbol=“£”},
新货币{CurrencyID=2,CurrencySymbol=“$”},
新货币{CurrencyID=3,CurrencySymbol=“$”}
};
}
捕获(例外)
{
}
}
}
}
如何使第一个元素(货币符号)成为默认元素?谢谢。首先,让您的选择器xaml代码如下
<Label Grid.Row="2" Grid.Column="0" Text="Currency Symbol" Margin="5,0,0,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>
现在您可以获得预期的输出。请参阅随附的视频
首先,让您的选择器xaml代码如下
<Label Grid.Row="2" Grid.Column="0" Text="Currency Symbol" Margin="5,0,0,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>
现在您可以获得预期的输出。请参阅随附的视频
代替设置选择的索引
,您是否尝试过绑定和设置选择器的SelectedItem
属性?您的Page1构造函数缺少对InitializeComponent
的调用-如果没有该行,您的代码甚至不应该生成。
是CurrencyList
的第一个索引,然后您需要设置CurrencyPicker.SelectedIndex=0代码>您好,我尝试过共享代码,它在我的本地站点上运行。如果您有时间,可以在此处共享一个示例项目链接。然后我将检查它。不设置SelectedIndex
,您是否尝试过绑定和设置选择器的SelectedItem
属性?您的Page1构造函数缺少对InitializeComponent
的调用-如果没有该行,您的代码甚至不应该生成。
是CurrencyList
的第一个索引,然后您需要设置CurrencyPicker.SelectedIndex=0代码>您好,我尝试过共享代码,它在我的本地站点上运行。如果您有时间,可以在此处共享一个示例项目链接。那我来核对一下。谢谢你的回答。我遇到了以下错误:CS0117:“对象”在base.RaisePropertyChanged(nameof(CurrencyList))行中不包含“RaisePropertyChanged”的定义代码>。有没有办法解决这个问题?再次感谢。谢谢你的回答。我遇到了以下错误:CS0117:“对象”在base.RaisePropertyChanged(nameof(CurrencyList))行中不包含“RaisePropertyChanged”的定义代码>。有没有办法解决这个问题?再次感谢。
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));
}
}
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();
}
protected override async Task Initialize(NavigationParameters parameters)
{
if (parameters.GetNavigationMode() == NavigationMode.New)
{
CurrencyList_Completed();
}
}