[英]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 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. 您正在创建一个新的本地companyViewSource,但实际上并没有绑定它。 I think you should be using a ObjectDataProvider .Change your code as following:
我认为您应该使用ObjectDataProvider。更改您的代码,如下所示:
XAML 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
因为
ObjectDataProvider
是一个调用GetFoundryDatabases
如您在MethodName
看到的那样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.