mjordan
mjordan

Reputation: 369

MVVM, using Multiple DataSets and Multiple ComboBoxes tied to each other

I have a single Database EstimateInformationTable with CATEGORY, DESCRIPTION. 100 records total. There are 100 Descriptions and 10 Categories, so obviously the Categories are used more than once. example:

+----------+-------------+
| CATEGORY | DESCRIPTION |
+----------+-------------+
| ACC      | DescAAAA    |
| ACC      | DescBBBB    |
| BMX      | DescCCCC    |
+----------+-------------+

I want to use the first ComboBox (CATEGORY) to limit the choices in the second ComboBox (DESCRIPTION).

I have two ComboBoxes bound to two DataSet TableAdaptors. Each TableAdaptor returns a simple, but different Schema.

ComboBox1:

SELECT DISTINCT CATEGORY
FROM EstimateInformationTable
ORDER BY CATEGORY

ComboBox2:

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

I can kind of get this to work in CodeBehind using an extra ButtonClickEvent, but obviously this is not ultimately what I want.

I want to use MVVM and the correct Notify Properties (if that is what needs to be done) so that changing the Category ComboBox updates the Description ComboBox.

This must be done all the time, but I am new to a lot of this. I was making progress, but I am having a hard time wrapping my head around this. This is a self imposed learning exercise, so feel free to dumb the explanation down for me. Thanks, mike

XAML:

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

CurrentCodeBehind

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");
        }
    }
}

Upvotes: 0

Views: 41

Answers (1)

mjordan
mjordan

Reputation: 369

I've found that this works, but I am curious if it is the best answer.

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

        }

Upvotes: 0

Related Questions