这里我将通过wpf中的数据表绑定组合框但是在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
{
}
}
答案 0 :(得分:0)
如果我理解得好PROFILE_NAME
只是结果中的一栏。当ItemsSource
绑定属性PROFILE_NAME
时,需要将其设置为某个IEnumerable
。您需要将其设置为DataTable
的某些视图,以便
ddlCategoryType.ItemsSource = dtGameProfileList.DefaultView;
或
ddlCategoryType.ItemsSource = new DataView(dtGameProfileList);
答案 1 :(得分:0)
考虑到WPF将在渲染发生之前尝试创建绑定,您可能需要考虑使用ObservableCollection。
在你的xaml中,你会将ItemsSource更新为ItemsSource =&#34; {Binding YourClassInstanceMember}&#34;然后在GameProfileList(..)中,您将非可观察集合类转换为ObservableCollection并将其分配给您的后备字段(随后将通知UI进行更新)。
一些推荐阅读
[数据绑定概述] 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();
}
}