简体   繁体   中英

Binding a combobox in wpf c# to a list<t> not displaying

I have a (should be) simple project where I need to create a list of three databases which are the same structure stored on different servers, each database represents a different Business site within a group of companies

I am building a data access layer which can be configured at runtime by the user. This will be achieved by a list of objects called Databases and the user selecting the company name of the database they wish to update. The first problem is I need to bind a combobox to my list of Databases, which I have done below. It does not error when I compile but it does not display either.

I am missing something obvious here please help

Many thanks

The xmal

 <Window.Resources>
    <CollectionViewSource x:Key="companyViewSource"/>

</Window.Resources>
<Grid>

    <Grid Height="38" HorizontalAlignment="Left" Margin="11,79,0,0" Name="grid2" VerticalAlignment="Top" Width="262">
        <ComboBox Height="23" HorizontalAlignment="Left" Margin="136,9,0,0" Name="cBComapny" VerticalAlignment="Top" Width="120" 
                    ItemsSource="{Binding Source={StaticResource companyViewSource}}"
                    DisplayMemberPath="CompanyName" 
                    SelectedValuePath="CompanyName" 
                    SelectedValue="{Binding CompanyLetter}" />
    </Grid>
</Grid>

The class code for Database

public class Databases
{
    public string Database { get; set; }
    public string CompanyName { get; set; }
    public string ServerName { get; set; }
    public string CompanyLetter { get; set; }

    public Databases()
    {

    }

    public static List<Databases> GetFoundryDatabases()
    {
        List<Databases> Foundries = new List<Databases>();

        Foundries.Add (new Databases(){Database="CompanyA", CompanyName="Company1", ServerName="Server1", CompanyLetter="A"});
        Foundries.Add(new Databases() { Database = "CompanyL", CompanyName = "Company2", ServerName = "Server1", CompanyLetter = "L" });
        Foundries.Add(new Databases() { Database = "CompanyR", CompanyName = "Company3", ServerName = "Server2", CompanyLetter = "R"});

        return Foundries;

    }

}

And the code for Window load

    System.Windows.Data.CollectionViewSource companyViewSource = new CollectionViewSource();
            companyViewSource.Source=SysproDAL.Databases.GetFoundryDatabases();
            companyViewSource.View.MoveCurrentToFirst();

You are creating a new local companyViewSource but you are really not binding it. I think you should be using a ObjectDataProvider .Change your code as following:

XAML

    xmlns:dal="clr-namespace:SysproDAL;assembly:SysproDAL"

    <Window.Resources>
        <ObjectDataProvider x:Key="DataBasesDataProvider"
            ObjectType="{x:Type dal:Databases}" MethodName="GetFoundryDatabases"/>
    </Window.Resources>
    <Grid>

        <Grid Height="38" HorizontalAlignment="Left" Margin="11,79,0,0" Name="grid2" VerticalAlignment="Top" Width="262">
            <ComboBox Height="23" HorizontalAlignment="Left" Margin="136,9,0,0" Name="cBComapny" VerticalAlignment="Top" Width="120" 
                ItemsSource="{Binding Source={StaticResource DataBasesDataProvider}}"
                DisplayMemberPath="CompanyName" 
                SelectedValuePath="CompanyName" 
                SelectedValue="{Binding CompanyLetter}" />
        </Grid>
    </Grid>

This way you can delete your Window Load Code. as the ObjectDataProvider is the one calling GetFoundryDatabases as you can see in MethodName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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