从组合框wpf ef6中的不同表创建拾取列表

从组合框wpf ef6中的不同表创建拾取列表,wpf,combobox,Wpf,Combobox,就在前面,我是个新手。我正在创建一个窗口,其中有许多组合框用作拾取列表。我在wpf ef6应用程序中有xaml和自动生成文件的代码。我有一个带有InspectionId(Int)和InspectionName(String)属性的表(InspectionTypes)。在我的估计窗口中,我有一个组合框,我希望列出和查看/选择一个InspectionTypeName,并将InspectionId存储在记录中 以下是组合框的XAML部分: { <ComboBox x:Name

就在前面,我是个新手。我正在创建一个窗口,其中有许多组合框用作拾取列表。我在wpf ef6应用程序中有xaml和自动生成文件的代码。我有一个带有InspectionId(Int)和InspectionName(String)属性的表(InspectionTypes)。在我的估计窗口中,我有一个组合框,我希望列出和查看/选择一个InspectionTypeName,并将InspectionId存储在记录中

以下是组合框的XAML部分:

{           <ComboBox x:Name="inspectionType1ComboBox" 
                  Grid.Column="2" Grid.Row="1" Height="Auto" Width="200" Margin="3"
                  HorizontalAlignment="Left" VerticalAlignment="Center"
                  ItemsSource="{Binding}"
                  DisplayMemberPath="InspectionType1">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel/>
                </ItemsPanelTemplate>
            </ComboBox.ItemsPanel>
        </ComboBox>
}
{
}
我一直在尝试使用LINQ查询和ObservableCollection。当前,我收到异常:System.FormatException:“输入字符串的格式不正确。”

此异常最初是在此调用堆栈中引发的: System.Number.StringToNumber(字符串,System.Globalization.NumberStyles,参考System.Number.NumberBuffer,System.Globalization.NumberFormatInfo,bool) System.Number.ParseInt32(字符串,System.Globalization.NumberStyles,System.Globalization.NumberFormatInfo) string.System.IConvertible.ToInt32(System.IFormatProvider) System.Convert.ChangeType(对象、System.Type、System.IFormatProvider) MS.Internal.Data.SystemConvertConverter.Convert(对象、系统、类型、对象、系统、全球化、文化信息) MS.Internal.Data.DynamicValueConverter.Convert(对象、系统、类型、对象、系统、全球化、文化信息) System.Windows.Controls.Primitives.Selector.VerifyEqual(对象、系统、类型、对象、MS.Internal.Data.DynamicValueConverter) System.Windows.Controls.Primitives.Selector.FindItemWithValue(对象,out int) System.Windows.Controls.Primitives.Selector.SelectItemWithValue(对象,布尔) System.Windows.Controls.Primitives.Selector.OnItemChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs)

代码隐藏:

private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        System.Windows.Data.CollectionViewSource estimatesViewSource = 
           ((System.Windows.Data.CollectionViewSource)(this.FindResource("estimatesViewSource")));
        System.Windows.Data.CollectionViewSource inspectionTypesViewSource = 
            ((System.Windows.Data.CollectionViewSource) 
            (this.FindResource("inspectionTypesViewSource")));
        context.Estimates.Load();
        estimatesViewSource.Source = context.Estimates.Local;
        inspectionTypesViewSource.Source = context.InspectionTypes.Local;
        FillComboBoxes();
    }
    private void FillComboBoxes()
    {
        MIDatabase01Entities1 db = new MIDatabase01Entities1();

        //var ItemList = (from d in db.InspectionTypes select new { d.InspectionTypesID, 
             d.InspectionTypeName }).ToList();
        var observablelist = new ObservableCollection<InspectionType>(db.InspectionTypes);

        inspectionType1ComboBox.DisplayMemberPath = "InspectionTypeName";
        inspectionType1ComboBox.SelectedValuePath = "InspectionTypesID";
        inspectionType1ComboBox.SelectedValue = "InspectionTypesID";
        //inspectionType1ComboBox.ItemsSource = ItemList;
        inspectionType1ComboBox.ItemsSource = observablelist;
    }
private void Window\u已加载(对象发送方,路由目标)
{
System.Windows.Data.CollectionViewSource estimatesViewSource=
((System.Windows.Data.CollectionViewSource)(this.FindResource(“estimatesViewSource”));
System.Windows.Data.CollectionViewSource检查类型ViewSource=
((System.Windows.Data.CollectionViewSource)
(这是FindResource(“inspectionTypesViewSource”));
context.Estimates.Load();
estimatesViewSource.Source=context.Estimates.Local;
inspectionTypesViewSource.Source=context.InspectionTypes.Local;
填充组合框();
}
私有void fillComboBox()
{
MIDatabase01Entities1 db=新的MIDatabase01Entities1();
//var ItemList=(从db.InspectionTypes中的d选择新的{d.InspectionTypesID,
d、 InspectionTypeName}.ToList();
var observablelist=新的ObservableCollection(db.InspectionTypes);
inspectionType1ComboBox.DisplayMemberPath=“InspectionTypeName”;
inspectionType1ComboBox.SelectedValuePath=“InspectionTypesID”;
inspectionType1ComboBox.SelectedValue=“InspectionTypesID”;
//检查类型1 ComboBox.ItemsSource=项目列表;
检查类型1 ComboBox.ItemsSource=可观察列表;
}

几个星期以来,我一直在努力寻找答案,但没有成功。请您帮忙好吗?

不要在方法中使用ItemsSource从xaml.cs设置组合框。您可以为相同的对象创建一个属性,并将其绑定到xaml中。出现此问题是因为您在渲染视图之前指定了数据

public partial class MainWindow : Window
{
    public ObservableCollection<InspectionType> observablelist { get; set; 

    public MainWindow()
    {
        InitializeComponent();
        System.Windows.Data.CollectionViewSource cvs = ((System.Windows.Data.CollectionViewSource)(this.FindResource("cvs")));
        DataContext = this;
        FillComboBoxes();
    }

    private void FillComboBoxes()
    {

        observablelist = new ObservableCollection<InspectionType>();
        observablelist.Add(new InspectionType()
        {
            InspectionTypeName = "ABC",
            InspectionTypesID = 1
        });

    }
}
公共部分类主窗口:窗口
{
公共可观测集合可观测列表{get;set;
公共主窗口()
{
初始化组件();
System.Windows.Data.CollectionViewSource cvs=((System.Windows.Data.CollectionViewSource)(this.FindResource(“cvs”));
DataContext=this;
填充组合框();
}
私有void fillComboBox()
{
observablelist=新的ObservableCollection();
添加(新的检查类型()
{
InspectionTypeName=“ABC”,
InspectionTypesID=1
});
}
}
xaml.cs

      <Window x:Class="WpfApp1.MainWindow"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    xmlns:dat="clr- 
       namespace:System.Windows.Data;assembly=PresentationFramework"
    xmlns:local="clr-namespace:WpfApp1"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="500" 
    Loaded="Window_Loaded">
   <Window.Resources>

    <CollectionViewSource Source="{Binding observablelist}"
                          x:Key="cvs">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="InspectionTypeName" />
        </CollectionViewSource.SortDescriptions>
        <CollectionViewSource.GroupDescriptions>
            <dat:PropertyGroupDescription PropertyName="InspectionTypesID" />
        </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>

</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="50" />
        <RowDefinition Height="auto" />
    </Grid.RowDefinitions>
    <ListBox ItemsSource="{Binding Source={StaticResource cvs}}"
             DisplayMemberPath="InspectionTypeName"
             Name="lb">
        <ListBox.GroupStyle>
            <x:Static Member="GroupStyle.Default" />
        </ListBox.GroupStyle>
    </ListBox>
    <ComboBox Grid.Row="2"
              Height="100"
              AllowDrop="True"
              Width="100"
              DisplayMemberPath="InspectionTypeName"
              SelectedValuePath="InspectionTypesID"
              ItemsSource="{Binding observablelist}"></ComboBox>


  </Grid>
 </Window>


我已经做了您建议的修改,我希望在组合框列表中看到“ABC”。它显示一个空白列表。我需要的是InspectionTypes表中的列表。按照建议添加每个项目是不可接受的。当我在FillComboBox()的右括号中放置断点时方法并查看项目计数,它为零。InspectionId是Estimates表中的外键,也是InspectionTypes表中的主键。下面是代码隐藏的当前代码部分:查看数据时,我可以看到ObserveryST中有一个条目显示添加的数据。我不明白为什么它不包含来自InspectionTypes表的数据。应将.Add方法添加到现有列表中。我不理解最后一行的作用。“inspectionType1ComboBox=new ComboBox();”组合框绑定到observablelist,但是数据中没有列出任何项目-令人困惑。它是否应该在项目列表中显示添加的项目?我确实理解在呈现之前需要显示数据-感谢这些信息。更新:我让observablelist与列表中适当数量的项目一起运行。组合框项目但是列表为空。我完全按照您的指示设置绑定,但是ItemsSource-null和ItemsCount=0。您可以共享您的代码吗?很抱歉,最后一行完全不需要。我希望您正确地提供了数据上下文。我如何发布代码?我应该编辑问题吗?