简体   繁体   English

WPF绑定到组合框

[英]WPF Binding to Combo Box

Hi a quick question really in WPF. 您好,真的在WPF中有一个简短的问题。

Does anyone know how i can bind a combo box that will sit in a grid to a dataset that i have. 有谁知道我如何将一个组合框(该组合框位于网格中)绑定到我拥有的数据集。

The dataset contains several columns and numerious rows but i want to take one of the columns, lets say ProcessID and just show that in the comobo box. 数据集包含几列和无数行,但是我想选择其中一列,让我们说一个ProcessID,然后在comobo框中显示出来。

Has anyone done this before or know how it can be done in WPF. 有没有人做过此事,或者知道如何在WPF中完成。

Thanks Iffy. 谢谢Iffy。

Assuming this is a capital D DataSet that you can expose to XAML as a property on a DataContext object (showing the "Description" column for each row in the "LookupTable" table): 假设这是一个大写的D数据集,您可以将它作为DataContext对象的属性公开给XAML(显示“ LookupTable”表中每一行的“ Description”列):

    <ComboBox ItemsSource="{Binding Path=MyDataSetExposedAsAProperty.Tables[LookupTable].Rows}"
DisplayMemberPath=".[Description]"/>

To do it programmatically, here's a generic function I have for it: 要以编程方式执行此操作,这是我要执行的通用功能:

    /// <summary>
    /// Thread safe method for databinding the specified ComboBox with the specified data.
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="ctrl">The Combo to be databound.</param>
    /// <param name="datasource">The data to be bound to the Combo.</param>
    /// <param name="displayPath">The name of the property in the data objects that should be used for the display value.</param>
    /// <param name="valuePath">The name of the property in the data objects that should be used for the value.</param>
    /// <remarks>
    /// This function was written as generic so that it doesn't matter what types the IEnumerabe datasource is, it can handle them all.
    /// </remarks>
    private void UpdateComboDataSource<T>(ComboBox ctrl, IEnumerable<T> datasource, string displayPath, string valuePath)
    {
        if (ctrl == null)
            throw new ArgumentNullException("Control to be bound must not be null");

        //check if we are on the control's UI thread:
        if (ctrl.Dispatcher.CheckAccess())
        {
            //we have full access to the control, no threading issues, so let's rip it up and databind it
            datasource = datasource.OrderBy(x => x);
            if (displayPath != null && ctrl.DisplayMemberPath == null)
                ctrl.DisplayMemberPath = displayPath;
            if (valuePath != null && ctrl.SelectedValuePath == null)
                ctrl.SelectedValuePath = valuePath;

            ctrl.ItemsSource = datasource;

            //be nice to the user, if there is only one item then automatically select it
            ctrl.SelectedIndex = datasource.Count() == 1 ? 0 : -1;
        }
        else
        {
            //we don't have full access to the control because we are running on a different thread, so 
            //we need to marshal a call to this function from the control's UI thread
            UpdateComboDataSourceDelegate<T> del = new UpdateComboDataSourceDelegate<T>(this.UpdateComboDataSource);
            ctrl.Dispatcher.BeginInvoke(del, ctrl, datasource, displayPath, valuePath);
        }
    }

    private delegate void UpdateComboDataSourceDelegate<T>(ComboBox ctrl, IEnumerable<T> dataSource, string displayPath, string valuePath);

The comments tell you everything you need to know. 这些评论会告诉您所有您需要知道的内容。 It could also be done programmatically by creating a Binding between the control's ItemSource property and a public property on your page - this is the same way as it is done declaratively (there must be a billion examples of that out there though, so I won't show it here). 也可以通过在控件的ItemSource属性和页面上的公共属性之间创建Binding来以编程方式完成-这与声明性完成的方式相同(尽管那里必须有十亿个示例,所以我不会这样做)在这里显示)。

您应该使用LINQ to SQL,ADO.Net或实体框架从数据库中提取数据,您不能将组合框与数据库表绑定,但是将数据存储在内存中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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