Telerik RadComboBox WPF如何显示数据库表中的值列表?

时间:2016-06-03 05:16:04

标签: c# wpf mvvm telerik

我在MVF中使用MVVM并且在我的视图中有一个RadComboBox需要从我的数据库中的County表中填充。我的viewmodel如下:

 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加载了几个值。如何更正上述代码段以确保填充我的县名?

更新:当我从County.CountyName中删除县时,我收到一条消息,指出Cannot resolve property CountyName in DataContext MySolution.ViewModels.MyViewModel在LoadCounty或其他部分的视图模型中还需要做哪些其他工作?

1 个答案:

答案 0 :(得分:1)

我建议如下:

介绍将包含County个对象列表的ViewModel属性:

private List<County> _counties;
public List<County> Counties
{
    get { return _counties;}
    set
    {
        if (value != _counties)
        {
            _counties = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Counties"));
        }
    }
}

将一个ComboBox ItemsSource绑定到Counties属性,将一个ComboBox SelectedItem属性绑定到County属性。

<telerik:RadComboBox x:Name="Countycombo" 
 Grid.Column="1" Grid.Row="3" 
 ItemsSource="{Binding Counties}" 
 SelectedItem="{Binding County}"
 DisplayMemberPath="CountyName" Width="120"/>

您需要在一个地方加载县,并通过存储库调用GetCountiesAsync。结果应设置为ViewModel Counties属性。

  public async void LoadCounties()
  {
     Counties = await _countyRepository.GetCountiesAsync();
  }

不确定拨打该电话的最佳位置是什么。