Click here to Skip to main content
16,022,362 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi Team

I have a combo box and its not loading the values it fetches from the table in the database, but its only showing on the grid and rectangle, when i click combo box its blank with nothing to select.

What I have tried:

XML
<UserControl.Resources>
        <CollectionViewSource x:Key="StockCodes" Source="{Binding StockCodes}"/>
        <CollectionViewSource x:Key="StockDescription" Source="{Binding Description}"/>
    </UserControl.Resources>
    
    <Grid Background="White" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Margin="0,86,-775.6,-46.6" Width="1766">
        <Label Content="Premix Material Management" BorderThickness="1.5" BorderBrush="#FF0B0B0B" Background="#FF383838" Foreground="White" FontSize="18" HorizontalContentAlignment="Center" VerticalAlignment="Top" Margin="10,10,0,0" FontWeight="SemiBold" />
         
        <!---Rectangle for Recipe Details-->
        <Rectangle  HorizontalAlignment="Left" Height="371" Stroke="#FFCFCFD1" VerticalAlignment="Top" Width="260" Margin="14,104,0,0"/>
        
        
        <!---Recipe fields-->
        <Label Content="Recipe Details" FontWeight="SemiBold"  HorizontalAlignment="Left" HorizontalContentAlignment="Center" VerticalAlignment="Top" Width="260" BorderBrush="#FFBEBEBE" Background="#FFE3E3E3" BorderThickness="1.5" Foreground="Black" Height="30" VerticalContentAlignment="Center" FontSize="12" Margin="14,103,0,0"/>
        <Label x:Name="lblActive_Copy9" FontWeight="SemiBold" FontSize="10" Visibility="Visible" Content="IDX:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,135,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtIDX" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" FontSize="10" Height="26" Margin="146,132,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy1" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Recipe:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,163,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtRecipe" Foreground="Black" FontSize="10" HorizontalAlignment="Left" Visibility="Visible" Height="26" Margin="146,160,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy13" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Rec No:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,191,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtRecNo" FontSize="10" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" Height="26" Margin="146,188,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy2" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Description:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,220,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtDescription" FontSize="10" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" Height="26" Margin="146,217,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy3" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Batch Size:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,248,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtBatchSize" FontSize="10" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" Height="26" Margin="146,245,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy4" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Palleting Instruction:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,276,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtPalletInstruction" FontSize="10" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" Height="26" Margin="146,273,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy5" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Feed First Recipe Name:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,304,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtFeedFirstName" FontSize="10" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" Height="26" Margin="146,301,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy11" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="PPA TK601:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,331,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtPPATK601" ToolTip="Can only be edit if second last digit in recipe is 2"  IsReadOnly="True" TextChanged="txtPPATK601_TextChanged" FontSize="10" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" Height="26" Margin="146,328,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy12" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="PPA TK602:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,359,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <TextBox x:Name="txtPPATK602" FontSize="10" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" Height="26" Margin="146,354,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy10" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Active:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,387,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <CheckBox x:Name="chkActive" TabIndex="3" HorizontalAlignment="Left" Checked="chkActive_Checked" Unchecked="chkActive_Unchecked" Margin="146,392,0,0" VerticalAlignment="Top" RenderTransformOrigin="-0.043,-1.119"/>
        <Button x:Name="btnAdd_Copy" Content="Save" Click="btnAdd_Click"  Background="ForestGreen" BorderBrush="ForestGreen" FontWeight="SemiBold" Visibility="Visible" HorizontalAlignment="Left" Margin="176,415,0,0" VerticalAlignment="Top" Width="90" />


        
        <!---Ingredient Totals-->
        <Rectangle  HorizontalAlignment="Left" Height="371" Stroke="#FFCFCFD1" VerticalAlignment="Top" Width="270" Margin="290,103,0,0"/>
        <Label Content="Ingredient Totals" FontWeight="SemiBold" HorizontalAlignment="Left" HorizontalContentAlignment="Center" VerticalAlignment="Top" Width="270" BorderBrush="#FFBEBEBE" Background="#FFE3E3E3" BorderThickness="1.5" Foreground="Black" Height="29" VerticalContentAlignment="Center" FontSize="12" Margin="288,104,0,0"/>
        
        <!---Ingredient fields names-->
        <Label x:Name="lblActive_Copy" FontWeight="SemiBold" Visibility="Visible" Content="Micros:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="124" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="296,135,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE" Height="23" FontSize="10"/>
        <TextBox x:Name="txtMicroTotal" FontSize="10" Foreground="Black" HorizontalAlignment="Left" Visibility="Visible" Height="20" Margin="425,138,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="120"/>
        <Label x:Name="lblActive_Copy7" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Macros:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="125" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="296,163,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE" Height="23"/>
        <TextBox x:Name="txtMacroTotal" Foreground="Black" FontSize="10" HorizontalAlignment="Left" Visibility="Visible" Height="23" Margin="426,163,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="122"/>
        <Label x:Name="lblActive_Copy8" FontSize="10" FontWeight="SemiBold" Visibility="Visible" Content="Total:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="125" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="295,191,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE" Height="23"/>
        <TextBox x:Name="txtTotal" Foreground="Black" FontSize="10" HorizontalAlignment="Left" Visibility="Visible" Height="23" Margin="426,191,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="122"/>


        <pre lang="XML"><!---Recipe name and drop down box-->
        <Label x:Name="lblDriverName_Copy" FontWeight="SemiBold" Visibility="Visible" Content="Recipe Code:" HorizontalAlignment="Left" VerticalAlignment="Top" Width="179" HorizontalContentAlignment="Right" Background="#FFE3E3E3" Margin="17,65,0,0" BorderThickness="1" BorderBrush="#FFBEBEBE"/>
        <ComboBox x:Name="cbxRecipe" TabIndex="1" IsTextSearchEnabled="True"  ItemsSource="{Binding Recipe}" DisplayMemberPath="StockCodes"  IsEditable="True"    IsTextSearchCaseSensitive="False"  Foreground="Black" HorizontalAlignment="Left" Margin="207,63,0,0" VerticalAlignment="Top" Width="214" SelectionChanged="cbxRecipe_SelectionChanged">
            <ComboBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <VirtualizingStackPanel />
                </ItemsPanelTemplate>
            </ComboBox.ItemsPanel>
        </ComboBox>
C#
private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadControl();

        }
        public void LoadControl()
        {
            Managers.BatchMaterialManager.SetDefaultConnection();
            presenter.PopulateComboBox();
            Properties.Settings.Default.Process = "Recipes";
            string[] arrayOil = RecipeName.Split('/');
            string OilIndicator = arrayOil[arrayOil.Length - 2].ToString();
            string LastDigit = RecipeName[RecipeName.Length - 2].ToString();
            if (OilIndicator == "2") // Has both oils in - allow split
            {
                txtPPATK601.IsReadOnly = false;
            }
        }
        private void cbxRecipe_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            LoadRecipe();
        }

        public void LoadRecipe()
        {
            TotalCrudeOilPerc = 0;
            CrudeOilPerc = 0;

            CollectionViewSource _StockCodeList = new CollectionViewSource();
            _StockCodeList = (CollectionViewSource)this.FindResource("StockCodes");

            CollectionViewSource _StockDescList = new CollectionViewSource();
            _StockDescList = (CollectionViewSource)this.FindResource("StockDescription");

            Managers.BatchMaterialManager.SetAdriotConnection();
            presenter.loadIngredients(Convert.ToInt32(cbxRecipe.SelectedValue), _StockCodeList, _StockDescList);
            presenter.PopulateTotals();
            presenter.loadRecipe(Convert.ToInt32(cbxRecipe.SelectedValue));

            string LastDigit = RecipeName.Substring(4, 1).ToString();
            if (LastDigit == "2") // Has both oils in - allow split
            {
                txtPPATK601.IsReadOnly = false;
            }

        }

  public void PopulateComboBox()
        {
            Managers.BatchMaterialManager.SetAdriotConnection();
            var Recipes = Managers.AdrBatchRecipeManager.GetCboRecipe();

            Recipes.DefaultView.Sort = "idx";
            View.ComboRecipe.ItemsSource = null;
            View.ComboRecipe.ItemsSource = Recipes.DefaultView;

            View.ComboRecipe.DisplayMemberPath = "StockCodes";
            View.ComboRecipe.SelectedValuePath = "idx";
            View.ComboRecipe.SelectedIndex = 1;
        }`
Posted
Updated 24-Jun-24 23:23pm
v2

You seem to be using a very cumbersome way of managing your combobox here. If I were you, I would be tempted to bind (once) to an extension of the ObservableCollection that manages bulk change notifications for you[^]. Then, when I was loading the data in, I would clear all the records if any were previously loaded, and add in the entirety of the data using AddRange. By doing this, you are bringing your application back into line with what WPF expects, and letting change notifications inform the UI when there are items present.

Also, don't do so much in the code-behind. You can set things like the ItemsSource, DisplayMemberPath, SelectedValuePath, and so on inside the XAML.
 
Share this answer
 
This tutorial website will show you a few different ways: The ComboBox control - The complete WPF tutorial[^].

Also, check out the rest of their tutorials.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900