2016-05-16 72 views
0

我有一个(应该是)简单的项目,我需要创建一个三个数据库的列表,这些数据库存储在不同服务器上的结构相同,每个数据库代表不同的业务在一组公司内的网站将wpf c#中的组合框绑定到列表<t>不显示

我正在构建一个数据访问层,可以在运行时由用户配置。这将通过名为数据库的对象列表和用户选择他们希望更新的数据库的公司名称来实现。第一个问题是我需要将一个组合框绑定到我的数据库列表,我在下面做了。我编译时不会出错,但它也不会显示。

我缺少明显的东西在这里,请帮助

非常感谢

的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; 

    } 

} 

类代码和窗口负载

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

回答

2

你正在创建一个新的本地companyViewSource,但你真的没有约束它。我想你应该使用ObjectDataProvider .Change代码如下:

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

+0

谢谢,但是当我做我得到以下错误**类型'本地:数据库'没有被发现。 在MS.Internal.Platform.MemberDocumentValueSerializer'1.ConvertToDocumentValue(ITypeMetadata型,字符串值,的IServiceProvider documentServices) 在MS.Internal.Design.DocumentModel.DocumentTrees.Markup.XamlMarkupExtensionPropertyBase.get_Value() 在MS.Internal.Design。 DocumentModel.DocumentTrees.DocumentPropertyWrapper.get_Value() ** –

+0

您必须为您的命名空间定义'local'。在我的情况下,它是'xmlns:local =“clr-namespace:WpfApplication2”',在您的'WpfApplication2'中更改为您的命名空间 – Pikoh

+0

'然而,我的方法是在调用SysproDAL中添加的项目