簡體   English   中英

組合框未在WPF中顯示值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM