[英]Combobox not displaying value in wpf
在这里,我将通过wpf中的数据表绑定组合框,但组合框未在fornt端显示值,任何人都可以知道这是怎么回事。这也是代码
//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
{
}
}
据我了解, PROFILE_NAME
只是您搜索结果中的一列。 当ItemsSource
需要设置为IEnumerable
时,它已绑定属性PROFILE_NAME
。 您需要将其设置为DataTable
某些视图,这样
ddlCategoryType.ItemsSource = dtGameProfileList.DefaultView;
要么
ddlCategoryType.ItemsSource = new DataView(dtGameProfileList);
考虑到WPF会在呈现之前尝试创建绑定,因此您可能要考虑使用ObservableCollection。
在您的xaml中,您将ItemsSource更新为ItemsSource =“ {Binding YourClassInstanceMember}”,然后在GameProfileList(..)中,将非可观察的集合类转换为ObservableCollection并将其分配给您的后备字段(随后会通知UI进行更新)。
一些推荐的阅读
[数据绑定概述] http://msdn.microsoft.com/zh-cn/library/ms752347(v=vs.110).aspx
[INotifyPropertyChanged]
http://msdn.microsoft.com/zh-CN/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();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.