MVVM,使用多个DataSet和多个ComboBox彼此绑定

时间:2017-11-24 16:12:43

标签: mvvm combobox dataset datatableadapters

我有一个数据库EstimateInformationTableCATEGORYDESCRIPTION。总共100条记录。有Descriptions和10 CategoriesCategories显然不止一次使用+----------+-------------+ | CATEGORY | DESCRIPTION | +----------+-------------+ | ACC | DescAAAA | | ACC | DescBBBB | | BMX | DescCCCC | +----------+-------------+ 。 例如:

TableAdaptors

我想使用第一个ComboBox(CATEGORY)来限制第二个ComboBox(DESCRIPTION)中的选项。

我有两个ComboBox绑定到两个DataSet TableAdaptor。每个SELECT DISTINCT CATEGORY FROM EstimateInformationTable ORDER BY CATEGORY 都返回一个简单但不同的Schema。

ComboBox1:

SELECT DESCRIPTION
FROM EstimateInformationTable
WHERE (CATEGORY = @Category)
ORDER BY DESCRIPTION

ComboBox2:

ButtonClickEvent

我可以使用额外的<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="clr-namespace:gobo.Pages" xmlns:gobo="clr-namespace:gobo" x:Class="gobo.Pages.TESTSTUFF01" xmlns:vm="clr-namespace:gobo.ViewModel" mc:Ignorable="d" Title="TESTSTUFF01" Loaded="Page_Loaded"> <Page.Resources> <vm:CategoryChangedViewModel x:Key="CategoryChangedViewModel"/> <gobo:gobo2018DataSet1 x:Key="gobo2018DataSet1"/> <CollectionViewSource x:Key="estimateInformationTableViewSource" Source="{Binding EstimateInformationTable, Source={StaticResource gobo2018DataSet1}}"/> <gobo:gobo2018EstimateInformationTableDescriptionDataSet x:Key="gobo2018EstimateInformationTableDescriptionDataSet"/> <CollectionViewSource x:Key="estimateInformationTableViewSource1" Source="{Binding EstimateInformationTable, Source={StaticResource gobo2018EstimateInformationTableDescriptionDataSet}}"/> </Page.Resources> <Page.Background> <LinearGradientBrush EndPoint="0.5,1" MappingMode="RelativeToBoundingBox" StartPoint="0.5,0"> <GradientStop Color="Black" Offset="0"/> <GradientStop Color="Red" Offset="1"/> </LinearGradientBrush> </Page.Background> <StackPanel Orientation="Vertical" > <Label Content="TEST STUFF 01 Tab Page1" VerticalAlignment="Top" Background="{x:Null}"/> <Button Content="Change Description contents" Click="Button_Click"/> <Grid x:Name="grid1" DataContext="{StaticResource estimateInformationTableViewSource}" HorizontalAlignment="Left" VerticalAlignment="Top"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Label Content="CATEGORY:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/> <ComboBox x:Name="cATEGORYComboBox" Grid.Column="1" DisplayMemberPath="CATEGORY" HorizontalAlignment="Left" Height="Auto" Margin="3" Grid.Row="0" VerticalAlignment="Center" Width="120" ItemsSource="{Binding}" SelectedIndex="{Binding Category, Source={StaticResource CategoryChangedViewModel}}"> <ComboBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel/> </ItemsPanelTemplate> </ComboBox.ItemsPanel> </ComboBox> </Grid> <Grid x:Name="grid2" DataContext="{StaticResource estimateInformationTableViewSource1}" HorizontalAlignment="Left" VerticalAlignment="Top" Width="311"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"/> <ColumnDefinition Width="Auto"/> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <Label Content="DESCRIPTION:" Grid.Column="0" HorizontalAlignment="Left" Margin="3" Grid.Row="0" VerticalAlignment="Center"/> <ComboBox x:Name="dESCRIPTIONComboBox" Grid.Column="1" DisplayMemberPath="DESCRIPTION" HorizontalAlignment="Left" Height="Auto" ItemsSource="{Binding}" Margin="3,9,-53,9" Grid.Row="0" VerticalAlignment="Center" Width="180"> <ComboBox.ItemsPanel> <ItemsPanelTemplate> <VirtualizingStackPanel/> </ItemsPanelTemplate> </ComboBox.ItemsPanel> </ComboBox> </Grid> </StackPanel> </Page> 在CodeBehind中使用它,但显然这不是我想要的。

我想使用MVVM和正确的通知属性(如果需要这样做),以便更改Category ComboBox更新Description ComboBox。

这必须一直这样做,但我是很多新手。我正在取得进展,但我很难绕过这个。这是一种自我强加的学习练习,所以请随意为我解释这些解释。谢谢,迈克

XAML:

namespace gobo.Pages
{
    /// <summary>
    /// Interaction logic for TESTSTUFF01.xaml
    /// </summary>
    public partial class TESTSTUFF01 : Page
    {
        private gobo.gobo2018DataSet1 gobo2018DataSet1;
        public gobo.gobo2018DataSet1TableAdapters.EstimateInformationTableTableAdapter gobo2018DataSet1TableAdapter;
        private CollectionViewSource estimateInformationTableViewSource;

        private gobo.gobo2018EstimateInformationTableDescriptionDataSet gobo2018EstimateInformationTableDescriptionDataSet;
        private gobo.gobo2018EstimateInformationTableDescriptionDataSetTableAdapters.EstimateInformationTableTableAdapter EstimateInformationTableTableAdapter;
        private CollectionViewSource estimateInformationTableViewSource1;


        public TESTSTUFF01()
        {
            InitializeComponent();
        }

        private void Page_Loaded(object sender, RoutedEventArgs e)
        {

            gobo2018DataSet1 = ((gobo.gobo2018DataSet1)(this.FindResource("gobo2018DataSet1")));
            gobo2018DataSet1TableAdapter = new gobo.gobo2018DataSet1TableAdapters.EstimateInformationTableTableAdapter();
            gobo2018DataSet1TableAdapter.Fill(gobo2018DataSet1.EstimateInformationTable);
            estimateInformationTableViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("estimateInformationTableViewSource")));
            estimateInformationTableViewSource.View.MoveCurrentToFirst();

            gobo2018EstimateInformationTableDescriptionDataSet = ((gobo.gobo2018EstimateInformationTableDescriptionDataSet)(this.FindResource("gobo2018EstimateInformationTableDescriptionDataSet")));
            EstimateInformationTableTableAdapter = new gobo.gobo2018EstimateInformationTableDescriptionDataSetTableAdapters.EstimateInformationTableTableAdapter();
            EstimateInformationTableTableAdapter.FillByCategory(gobo2018EstimateInformationTableDescriptionDataSet.EstimateInformationTable,"ACC");
            estimateInformationTableViewSource1 = ((System.Windows.Data.CollectionViewSource)(this.FindResource("estimateInformationTableViewSource1")));
            estimateInformationTableViewSource1.View.MoveCurrentToFirst();


        }

        public void LoadDescriptionToCbo(String parameter)
        {
            if(gobo2018EstimateInformationTableDescriptionDataSet != null)
            {
                EstimateInformationTableTableAdapter.FillByCategory(gobo2018EstimateInformationTableDescriptionDataSet.EstimateInformationTable, parameter);
            }

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            LoadDescriptionToCbo("ACT");
            Console.WriteLine("Button Click -> LoadDescriptionToCode");
        }
    }
}

CurrentCodeBehind

{{1}}

1 个答案:

答案 0 :(得分:0)

我发现这有效,但我很好奇它是否是最好的答案。

private void cATEGORYComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox cbx = (ComboBox)sender;
            string s = ((DataRowView)cbx.Items.GetItemAt(cbx.SelectedIndex)).Row.ItemArray[0].ToString();    
            LoadDescriptionToCbo(s);

        }