组合框未在 wpf 中显示值

本文关键字:显示 wpf 组合 | 更新日期: 2023-09-27 17:59:06

在这里,我将通过 WPF 中的数据表绑定组合框,但组合框在前端不显示值,任何人都可以说出这有什么问题。

//XAML
<ComboBox Canvas.Left="148" Canvas.Top="62" Height="23" 
          Name="cmbGameProfile" Width="217" 
          ItemsSource="{Binding Path=PROFILE_NAME}" 
          DisplayMemberPath="PROFILE_NAME"  />     
//Code-behind
public static void GameProfileList(ComboBox ddlCategoryType)
{
    try
    {
            DataTable dtGameProfileList = null;   
            try
            {
                ClsDataLayer objDataLayer = new ClsDataLayer();
                objDataLayer.AddParameter("@REF_USER_ID",0);
                dtGameProfileList = objDataLayer.ExecuteDataTable("COMNODE_PROC_GetGameProfile");
                if (dtGameProfileList != null && dtGameProfileList.Rows.Count > 0)
                {
                    ddlCategoryType.DataContext = dtGameProfileList;
                    ddlCategoryType.DisplayMemberPath = dtGameProfileList.Rows[0]["PROFILE_NAME"].ToString();
                    ddlCategoryType.SelectedValuePath = dtGameProfileList.Rows[0]["PROFILE_ID"].ToString();
                }
            }
            catch (Exception)
            {
                throw;
            }                                  
    }
    catch
    {
    }
}

组合框未在 wpf 中显示值

如果我理解得很好PROFILE_NAME只是您结果中的一列。目前ItemsSource是绑定属性PROFILE_NAME需要设置为某个IEnumerable。您需要将其设置为DataTable的某个视图,因此请这样做

ddlCategoryType.ItemsSource = dtGameProfileList.DefaultView;

ddlCategoryType.ItemsSource = new DataView(dtGameProfileList);

您可能需要考虑使用 ObservableCollection,因为 WPF 将尝试在呈现发生之前创建绑定。

在 xaml 中,将 ItemsSource 更新为 ItemsSource="{Binding YourClassInstanceMember}",然后在 GameProfileList(..( 中,将不可观察的集合类转换为 ObservableCollection,并将其分配给支持字段(随后会通知 UI 进行更新(。

一些推荐阅读

[数据绑定概述] http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110(.aspx

[不可信属性变更]

http://msdn.microsoft.com/en-us/library/vstudio/system.componentmodel.inotifypropertychanged

在伪代码中..

<Window xmlns:vm="clr-namespace:YourProject.YourViewModelNamespace">
    <Window.DataContext>
        <vm:YourViewViewModel />
    </Window.DataContext>
    <ComboBox ItemsSource="{Binding GameProfileListItems}"/>
</Window>
public class YourViewViewModel : ViewModelBase (this is something that implements INotifyPropertyChanged)
{
    ObservableCollection<string> _gameProfileListItems;
    ObservableCollection<string> GameProfileListItems
    {
        get { return _gameProfileListItems; }
        set { _gameProfileListItems = value; OnPropertyChanged("GameProfileListItems"); }
    }
    public void SetGameProfileListItems()
    {
        //    go and get your data here, transfer it to an observable collection
        //    and then assign it to this.GameProfileListItems (i would recommend writing a .ToObservableCollection() extension method for IEnumerable)
        this.GameProfileListItems = SomeManagerOrUtil.GetYourData().ToObservableCollection();
    }
}
public class YourView : Window
{
    public void YourView()
    {
        InitializeComponent();
        InitializeViewModel();
    }
    YourViewViewModel ViewModel
    {
        { get return this.DataContext as YourViewViewModel; }
    }
    void InitializeViewModel()
    {
        this.ViewModel.SetGameProfileListItems();
    }
}