WPF数据集 - 来自外键表的组合框

时间:2013-05-06 06:51:45

标签: wpf wpf-controls

我第一次涉足WPF和数据集,如果这个问题是基本的,那么道歉。

我的数据集中有一个Product Table和一个ProductSubCategory,我在两个表中基于ProductSubCategoryID列创建了一个关系。

在DataSources窗口中,我能够成功地将ProductTable中的列拖到我的表单上,并且我有代码可以让我浏览记录。我也可以进行更改并保存记录。一切正常

对于Product SubCategory,我将一个Combo框拖到窗体上,并将DisplayName设置为Description(来自ProductSubCategory表)。这也很好,这意味着当产品有产品时,它会显示正确的ProductSubCategory描述。但是组合框只有一个单独的条目

我还想做的是使用ProductSubcategory表中的所有条目填充组合框,以便用户可以根据需要选择不同的组合,并在保存产品记录时保存此项。

这是我页面的XAML

组合框名为“NameComboBox”

                                                                                                                         

    <Grid x:Name="Grid1" DataContext="{StaticResource ProductViewSource}" HorizontalAlignment="Left" Margin="71,36,0,0" Grid.Row="1" VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Content="Product ID:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/>
        <TextBox x:Name="ProductIDTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="0" Text="{Binding ProductID, Mode=OneWay}" VerticalAlignment="Center" Width="120"/>
        <Label Content="Name:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="1" VerticalAlignment="Center"/>
        <TextBox x:Name="NameTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="1" Text="{Binding Name, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
        <Label Content="Product Number:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="2" VerticalAlignment="Center"/>
        <TextBox x:Name="ProductNumberTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="2" Text="{Binding ProductNumber, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
        <Label Content="Make Flag:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="3" VerticalAlignment="Center"/>
        <CheckBox x:Name="MakeFlagCheckBox" Content="" Grid.Column="1" HorizontalAlignment="Left" IsChecked="{Binding MakeFlag, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" Margin="3" Grid.Row="3" VerticalAlignment="Center"/>
        <Label Content="Color:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="4" VerticalAlignment="Center"/>
        <TextBox x:Name="ColorTextBox" Grid.Column="1" HorizontalAlignment="Left" Height="23" Margin="3" Grid.Row="4" Text="{Binding Color, Mode=TwoWay, NotifyOnValidationError=true, ValidatesOnExceptions=true}" VerticalAlignment="Center" Width="120"/>
    </Grid>
    <Grid x:Name="Grid5" DataContext="{StaticResource ProductProductSubcategoryViewSource}" HorizontalAlignment="Left" Margin="208,214,0,0" Grid.Row="1" VerticalAlignment="Top">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Content="Name:" Grid.Column="0" HorizontalAlignment="Left" Margin="-133,-10,0,16" Grid.Row="0" VerticalAlignment="Center"/>
        <ComboBox x:Name="NameComboBox" DisplayMemberPath="Name" HorizontalAlignment="Left" Height="Auto" ItemsSource="{Binding}" Margin="-27,-10,0,20" Grid.Row="0" VerticalAlignment="Center" Width="120" Grid.ColumnSpan="2">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel/>
                </ItemsPanelTemplate>
            </ComboBox.ItemsPanel>
        </ComboBox>
    </Grid>
</Grid>

0 个答案:

没有答案