我需要在 wpf 和 mvvm 的组合框中填充货币列表。我尝试使用文化信息将货币列表添加到组合框中。这是视图模型中的代码
货币视图模型.cs
public List <CountryCurrencyPair> GetCountryList()
{
return CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(c => new RegionInfo(c.LCID)).Distinct()
.Select(r => new CountryCurrencyPair()
{
Country = r.EnglishName,
Currency = r.CurrencyEnglishName
}).ToList();
}
我正在下面的组合框中绑定ItemSource="{Binding GetCountryList}"。请参阅下面的组合框代码:
MainPage.xaml
<ComboBox Grid.Column="1" x:Name="cmbCurrency" Width="100"
HorizontalAlignment="Left" ItemsSource="{Binding GetCountryList}"
DisplayMemberPath="Country" SelectedValuePath="Currency"
SelectedValue="{Binding Currency, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"/>
但该列表不会填充组合框。
有没有人有上述问题的解决方案?
在绑定中使用 List 属性而不是方法
XAML
<ComboBox x:Name="cmbCurrency"
Grid.Column="1"
Width="100" Height="30"
HorizontalAlignment="Left"
DisplayMemberPath="Country"
ItemsSource="{Binding CurrencyList}"
SelectedValue="{Binding Currency,
Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"
SelectedValuePath="Currency" />
代码隐藏
public partial class MainWindow : Window
{
public List<CountryCurrencyPair> CurrencyList { get; set; }
public MainWindow()
{
InitializeComponent();
this.DataContext = this;
CurrencyList = GetCountryList();
}
public List<CountryCurrencyPair> GetCountryList()
{
return CultureInfo.GetCultures(CultureTypes.SpecificCultures)
.Select(c => new RegionInfo(c.LCID)).Distinct()
.Select(r => new CountryCurrencyPair()
{
Country = r.EnglishName,
Currency = r.CurrencyEnglishName
}).ToList();
}
}
public class CountryCurrencyPair
{
public string Country {get;set;}
public string Currency { get; set; }
}