我有一个(应该是)简单项目,我需要创建一个包含三个数据库的列表,这些数据库是存储在不同服务器上的相同结构,每个数据库代表一组公司内的不同商业站点
我正在构建一个数据访问层,可以在运行时由用户配置。这将通过称为数据库的对象列表以及用户选择他们希望更新的数据库的公司名称来实现。第一个问题是我需要将一个组合框绑定到我的数据库列表,我在下面做了。我编译时没有错误,但它也没有显示。
我遗漏了一些明显的东西,请帮忙
非常感谢
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>
数据库的类代码
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;
}
}
Window加载的代码
System.Windows.Data.CollectionViewSource companyViewSource = new CollectionViewSource();
companyViewSource.Source=SysproDAL.Databases.GetFoundryDatabases();
companyViewSource.View.MoveCurrentToFirst();
答案 0 :(得分:2)
您正在创建一个新的本地companyViewSource,但您确实没有绑定它。我认为你应该使用ObjectDataProvider。更改你的代码如下:
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>
这样您就可以删除窗口加载代码。正如您在ObjectDataProvider
GetFoundryDatabases
是MethodName