简体   繁体   中英

Combobox not displaying value in wpf

here im going to bind combobox through datatable in wpf but combobox not displaying value in fornt end can any one tell what the wrong with this.here is code also

//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
    {
    }
}

If I understand well PROFILE_NAME is just a column in your results. At the moment ItemsSource is bound property PROFILE_NAME when it needs to be set to some IEnumerable . You need to set it to some view of your DataTable so do

ddlCategoryType.ItemsSource = dtGameProfileList.DefaultView;

or

ddlCategoryType.ItemsSource = new DataView(dtGameProfileList);

You may want to consider using an ObservableCollection given that WPF will attempt to create your binding before the rendering occurs.

In your xaml you would update the ItemsSource to ItemsSource="{Binding YourClassInstanceMember}" and then in GameProfileList(..) you would convert your non-observable collection class to an ObservableCollection and assign it to your backing field (which would subsequently notify the UI to update).

Some recommended reading

[DataBinding overview] http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx

[INotifyPropertyChanged]

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

In pseudo code..

<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();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM