【发布时间】:2016-06-03 05:16:04
【问题描述】:
我将 MVVM 与 WPF 结合使用,并且在我的视图中有一个 RadComboBox,需要从我的数据库中的 County 表中填充。我的视图模型如下:
public class AddClientViewModel : BindableBase
{
private Client _client;
private Circuit _circuit;
private County _county;
private State _state;
private SubscriberSpecialty _subscriberSpecialty;
private IClientsRepository _repository = new ClientRepository();
private ICircuitRepository _circuitRepository = new CircuitRepository();
private ICountyRepository _countyRepository = new CountyRepository();
private IStateRepository _stateRepository = new StateRepository();
private ISubscriberSpecialty _subscriberSpecialtyRepository = new SubscriberSpecialtyRepository();
public AddClientViewModel()
{
SaveCommand = new RelayCommand(OnSave);
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
public Client Client
{
get { return _client; }
set
{
if (value != _client)
{
_client = value;
PropertyChanged(this, new PropertyChangedEventArgs("Client"));
}
}
}
public Circuit Circuit
{
get { return _circuit; }
set
{
if(value != _circuit)
{
_circuit = value;
PropertyChanged(this, new PropertyChangedEventArgs("Circuit"));
}
}
}
public County County
{
get { return _county;}
set
{
if (value != _county)
{
_county = value;
PropertyChanged(this, new PropertyChangedEventArgs("County"));
}
}
}
public State State
{
get { return _state; }
set
{
if (value != _state)
{
_state = value;
PropertyChanged(this, new PropertyChangedEventArgs("State"));
}
}
}
public SubscriberSpecialty SubscriberSpecialty
{
get { return _subscriberSpecialty; }
set
{
if (value != _subscriberSpecialty)
{
_subscriberSpecialty = value;
PropertyChanged(this, new PropertyChangedEventArgs("SubscriberSpecialty"));
}
}
}
public Guid ClientId { get; set; }
public Guid CircuitId { get; set; }
public Guid CountyId { get; set; }
public Guid StateId { get; set; }
public Guid SubscriberSpecialtyId { get; set; }
public ICommand SaveCommand { get; set; }
public event Action<Client> AddClient = delegate { };
public async void LoadClient()
{
Client = await _repository.GetClientAsync(ClientId);
}
public async void LoadCircuit()
{
Circuit = await _circuitRepository.GetCircuitAsync(CircuitId);
}
public async void LoadCounty()
{
County = await _countyRepository.GetCountyAsync(CountyId);
}
public async void LoadState()
{
State = await _stateRepository.GetStateAsync(StateId);
}
public async void LoadSubscriberSpecialty()
{
SubscriberSpecialty = await _subscriberSpecialtyRepository.GetSubscriberSpecialtyAsync(SubscriberSpecialtyId);
}
private void OnAddClient()
{
AddClient(new Client {ClientId = Guid.NewGuid()});
}
private async void OnSave()
{
try
{
Client = await _repository.AddClientAsync(new Client());
}
catch (Exception ex)
{
MessageBox.Show("A handled exception just occurred: " + ex.Message, "Exception", MessageBoxButton.OK,
MessageBoxImage.Warning);
}
}
}
界面有以下内容:
Task<County> GetCountyAsync(Guid countyId);
存储库类调用接口为:
public Task<List<County>> GetCountiesAsync()
{
return _context.Counties.ToListAsync();
}
然后我的视图使用以下语法:
<telerik:RadComboBox x:Name="Countycombo"
Grid.Column="1" Grid.Row="3"
ItemsSource="{Binding County.CountyName}"
DisplayMemberPath="CountyName" Width="120"/>
我在布局中定义了一个DataContext如下:
<UserControl.DataContext>
<viewModels:AddClientViewModel />
</UserControl.DataContext>
当我运行该应用程序时,RadComboBox 不会从 County 表中获取值,我已将 CountyName 的多个值加载到该表中。如何更正上述代码 sn-ps 以确保填写我的县名?
更新:当我从 County.CountyName 中删除 County 时,我收到一条消息,指出 Cannot resolve property CountyName in DataContext MySolution.ViewModels.MyViewModel 在 LoadCounty 或其他部分的 viewmodel 中需要做哪些额外的工作?
【问题讨论】:
-
从
ItemsSource="{Binding County.CountyName}"中删除.CountyName -
谢谢。如需进一步指导,请参阅更新。
-
包含 County 对象列表的 ViewModel 属性的名称是什么? GetCountiesAsync 的结果存储在哪里?
-
在
ItemsSource中,您有一个绑定对象集合(可能是GetCountiesAsync()的结果。DisplayMemberPath指示应显示列表中每个项目的哪个属性(例如 CountyName 或CountyId 等) -
@jure 我可能还没有设置视图模型来执行此操作。你有什么建议?