首页 > 解决方案 > 基于组合框选择填充 WPF 列表框

问题描述

我正在尝试使用基于组合框选择的 SQL 存储过程中的数据填充 WPF 列表框。我已经让 ComboBox 按预期工作,但我无法让 ListBox 显示任何数据。我的命名可能有点奇怪,但可以将其想象为:ComboBox 从 SQL 获取所有食谱,而 ListBox 需要根据用户从该 ComboBox 中的选择显示成分列表及其数量。API 和存储过程(...GetAll() 用于 ComboBox 和 GetByRationId() 用于 ListBox...)工作,因为我可以使用 API 中的 Swagger 检索正确的数据,并且可以填充 ComboBox 和 RationId TextBlock在 UI 中,但我无法让 ListBox 显示任何数据。我还是编程新手,我正在学习教程等,我可以 似乎找不到任何特别适合我的情况的东西。我猜我错过了一些东西。我添加了前面提到的 TextBlock 只是为了显示 RationId,这是从 SQL 获取正确数据所需要使用的,作为测试,只是为了确保 Id 正在通过......而且确实如此。

这是Xaml ...

    <StackPanel Grid.Column="1" Margin="50" Orientation="Vertical">
        <ComboBox x:Name="FeedGroup" MinWidth="300" MinHeight="50"
                  SelectedItem="{Binding SelectedFeedGroup}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding FeedGroupName}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
        
        <TextBlock x:Name="SelectedFeedGroup_RationId" Height="81"/>

        <ListBox x:Name="FeedGroupRation" MinHeight="200" Padding="20" ItemsSource="{Binding SelectedFeedGroupRation}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal" Margin="10" HorizontalAlignment="Center">
                        <TextBlock Text="{Binding CommodityName}" FontSize="20" FontWeight="Bold"
                            VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        <TextBlock Text="{Binding CommodityPercentage}" FontSize="16" VerticalAlignment="Center"/>
                    </StackPanel>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox> 
    </StackPanel>

这是 ViewModel 类...

public class FeedGroupPageViewModel : Screen
{

    IFeedGroupEndPoint _feedGroupEndPoint;
    IFeedGroupRationEndPoint _feedGroupRationEndPoint;
    IMapper _mapper;
    private readonly StatusInfoViewModel _status;
    private readonly IWindowManager _window;


    public FeedGroupPageViewModel(IFeedGroupEndPoint feedGroupEndPoint,
            IFeedGroupRationEndPoint feedGroupRationEndpoint,
            IConfigHelper configHelper,
            IMapper mapper,
            StatusInfoViewModel status,
            IWindowManager window)
    {
        _feedGroupEndPoint = feedGroupEndPoint;
        _feedGroupRationEndPoint = feedGroupRationEndpoint;
        _configHelper = configHelper;
        _mapper = mapper;
        _status = status;
        _window = window;
    }

    protected override async void OnViewLoaded(object view)
    {
        base.OnViewLoaded(view);
        try
        {
            await LoadFeedGroup();
        }
        catch (Exception ex)
        {

        }
    }

    private async Task LoadFeedGroup()
    {
        var FeedGroupList = await _feedGroupEndPoint.GetAll();
        var feedGroup = _mapper.Map<List<FeedGroupDisplayModel>>(FeedGroupList);
        FeedGroup = new BindableCollection<FeedGroupDisplayModel>(feedGroup);
    }

    private BindableCollection<FeedGroupDisplayModel> _feedGroup;
    public BindableCollection<FeedGroupDisplayModel> FeedGroup
    {
        get { return _feedGroup; }
        set
        {
            _feedGroup = value;
            NotifyOfPropertyChange(() => FeedGroup);
        }
    }

    private FeedGroupDisplayModel _selectedFeedGroup;
    public FeedGroupDisplayModel SelectedFeedGroup
    {
        get { return _selectedFeedGroup; }
        set
        {
            _selectedFeedGroup = value;
            NotifyOfPropertyChange(() => SelectedFeedGroup);
        }
    }



    private BindableCollection<FeedGroupRationModel> _feedGroupRation;
    public BindableCollection<FeedGroupRationModel> FeedGroupRation
    {
        get { return _feedGroupRation; }

        set
        {
            _feedGroupRation = value;
            NotifyOfPropertyChange(() => FeedGroupRation);
        }
    }

    private BindableCollection<FeedGroupRationModel> _selectedFeedGroupRation;
    public BindableCollection<FeedGroupRationModel> SelectedFeedGroupRation
    {
        get { return _selectedFeedGroupRation; }
        set
        {
            _selectedFeedGroupRation = value;
            NotifyOfPropertyChange(() => SelectedFeedGroupRation);
        }
    }
}

这是模型类

public class FeedGroupDisplayModel : INotifyPropertyChanged
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public string FeedGroupName { get; set; }
    public DateTime CreateDate { get; set; }
    public DateTime LastModified { get; set; }
    public int RationId { get; set; }



    public event PropertyChangedEventHandler PropertyChanged;
    public void CallPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}


public class FeedGroupRationModel : INotifyPropertyChanged
{
    public int Id { get; set; }
    public string UserId { get; set; }
    public int RationId { get; set; }
    public string RationName { get; set; }
    public int CommodityId { get; set; }
    public string CommodityName { get; set; }
    public int CommodityPercentage { get; set; }



    public event PropertyChangedEventHandler PropertyChanged;
    public void CallPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

这是我的端点类

public class FeedGroupEndPoint : IFeedGroupEndPoint
{
    private IAPIHelper _apiHelper;

    public FeedGroupEndPoint(IAPIHelper apiHelper)
    {
        _apiHelper = apiHelper;
    }

    public async Task<List<FeedGroupModel>> GetAll()
    {
        using (HttpResponseMessage response = await _apiHelper.ApiClient.GetAsync("/api/FeedGroup"))
        {
            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsAsync<List<FeedGroupModel>>();
                return result;
            }
            else
            {
                throw new Exception(response.ReasonPhrase);
            }
        }
    }
}

public class FeedGroupRationEndPoint : IFeedGroupRationEndPoint
{
    private IAPIHelper _apiHelper;

    public FeedGroupRationEndPoint(IAPIHelper apiHelper)
    {
        _apiHelper = apiHelper;
    }
    public async Task<List<FeedGroupRationModel>> GetRationById()
    {
        using (HttpResponseMessage response = await _apiHelper.ApiClient.GetAsync("/api/FeedGroup"))
        {
            if (response.IsSuccessStatusCode)
            {
                var result = await response.Content.ReadAsAsync<List<FeedGroupRationModel>>();
                return result;
            }
            else
            {
                throw new Exception(response.ReasonPhrase);
            }
        }
    }
}

如果需要,我可以添加更多信息。我已经为此工作了很长一段时间,但我只是没有想法。任何帮助将不胜感激!提前致谢!!

标签: c#sqlwpfxaml

解决方案


你似乎没有设置FeedGroupRation绑定ListBox到某个地方。

我猜您想在设置属性时获取项目并设置属性SelectedFeedGroup。然后,您可以将事件处理程序连接到PropertyChanged事件或覆盖该NotifyOfPropertyChange方法。像这样的东西:

public override async void NotifyOfPropertyChange([CallerMemberName] string propertyName = null)
{
    base.NotifyOfPropertyChange(propertyName);
    if (propertyName == nameof(FeedGroup))
    {
        //get the items...
        var results = await ...;
        //set the source property
        FeedGroupRation = results;
    }

}

推荐阅读