我正在开发一个C#/ WPF应用程序。 我点击按钮在MainViewModel中调用ProcessServiceResponse()方法。 SelectedCountry属性值在此方法中正确设置。国家/地区列表组合框还显示国家/地区列表。 但不知何故,我没有在国家/地区下拉列表中看到选定的值(例如SG)。 关于我在这里缺少什么的任何想法,请? 如果您需要有关代码的任何其他详细信息,请与我们联系。
感谢。
这是我的代码。
MainWindow View:
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:MainViewModel="clr-namespace:MyTool.ViewModels"
xmlns:ViewModel="clr-namespace:MyTool.ViewModel.Bonds"
xmlns:View="clr-namespace:MyTool" x:Class="MyTool.MainWindow"
Title="{Binding DisplayName, Mode=OneWay}" ResizeMode="CanMinimize" WindowStartupLocation="CenterScreen" Height="600" Width="1100">
<Window.DataContext>
<MainViewModel:MainWindowViewModel/>
</Window.DataContext>
<ComboBox Margin="1,0" ItemsSource="{Binding MyViewModel.CountryList,UpdateSourceTrigger=PropertyChanged}" DisplayMemberPath="Description" SelectedValuePath="Code" SelectedItem="{Binding MyViewModel.SelectedCountry, Mode=TwoWay}" TabIndex="2" Grid.Row="7" Grid.Column="2" HorizontalAlignment="Left" Height="23" VerticalAlignment="Top" Width="180" />
MainViewModel:
public MainWindowViewModel()
{
MyAttributes = new MyViewModel();
}
public object MyAttributes
{
get { return m_myViewModel; }
set
{
m_myViewModel = value;
OnPropertyChanged("MyAttributes");
}
}
public void ProcessServiceResponse()
{
var destination = new MyViewModel();/
Type destinationType = destination.GetType();
PropertyInfo[] destinationTypePI = destinationType.GetProperties();
string propertyName = string.Empty;
object propertyValue = null;
foreach (var pinfo in sourcePI)
{
propertyName = pinfo.Name.Trim();
var matchingItem = destinationTypePI.ToList().Where(d => d.Name == propertyName);
if (matchingItem != null && matchingItem.Count() > 0)
{
propertyValue = pinfo.GetValue(serviceResponse.lst_DKSecurities[0]);
matchingItem.FirstOrDefault().SetValue(destination, propertyValue);
}
}
this.MyAttributes = destination;
}
MyViewModel:
namespace MyTool.ViewModels;
public class MyViewModel
{
public MyViewModel
{
this.CountryList = GetCountryList();
}
public string SelectedCountry
{
get
{
return m_selectedCountry;
}
set
{
m_selectedCountry = value;
}
}
}
答案 0 :(得分:0)
填写CountryList后,将SelectedCountry分配给您要显示的列表项:
CountryList = new ObservableCollection<string> {"A", "B", "C"};
SelectedCountry = CountryList[0];
<ComboBox ItemsSource="{Binding CountryList, UpdateSourceTrigger=PropertyChanged}"
SelectedItem="{Binding SelectedCountry, Mode=TwoWay}" />