Комбобокс не отображает значение в wpf

Вопрос: здесь im собирается связывать combobox через datatable в wpf, но combobox, не отображающий значение в fornt end, может кто-нибудь сказать, что не так с этим. //XAML //Code-behind public static void GameProfileList(ComboBox ddlCategoryType) { try { DataTable dtGameProfileList = null; try { ClsDataLayer objDataLayer =

Вопрос:

здесь im собирается связывать combobox через datatable в wpf, но combobox, не отображающий значение в fornt end, может кто-нибудь сказать, что не так с этим.

//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 { } } Ответ №1

Если я хорошо понимаю, PROFILE_NAME – это всего лишь столбец в ваших результатах. На данный момент ItemsSource привязана к свойству PROFILE_NAME когда ему нужно установить некоторый IEnumerable. Вам нужно установить его в некоторый вид вашего DataTable так что

ddlCategoryType.ItemsSource = dtGameProfileList.DefaultView;

или

ddlCategoryType.ItemsSource = new DataView(dtGameProfileList); Ответ №2

Вы можете захотеть использовать ObservableCollection, учитывая, что WPF попытается создать привязку до того, как произойдет рендеринг.

В вашем xaml вы должны обновить ItemsSource до ItemsSource = “{Binding YourClassInstanceMember}”, а затем в GameProfileList (..) вы преобразуете свой невидимый класс коллекции в ObservableCollection и назначьте его в свое фоновое поле (которое впоследствии уведомит об этом UI для обновления).

Некоторые рекомендуемые чтения

[Обзор DataBinding] http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx

[INotifyPropertyChanged]

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

Оцените статью
Добавить комментарий