【问题标题】:Telerik RadComboBox WPF How to display list of values from database table?Telerik RadComboBox WPF 如何显示数据库表中的值列表?
【发布时间】: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 我可能还没有设置视图模型来执行此操作。你有什么建议?

标签: c# wpf mvvm telerik


【解决方案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();
  }

不确定拨打电话的最佳地点是什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 2010-09-28
    • 1970-01-01
    相关资源
    最近更新 更多