Populate dropdown upon selecting a value from another dropdown

Hi,

With the codes below, I'd like to populate another drop-down button, say on the top right by executing

"SELECT DISTINCT \"Delivery Month\" FROM Futures WHERE Name=Selected Market name from the center dropdown ORDER BY \"Delivery Month\"". I appreciate if you could please help me with a code snippet achieving this.

Country.cs:

namespace SyncfusionWpfDropdownTest
{
    public class Country
    {
        public string? Name { get; set; }
    }
}

Market.cs:

namespace SyncfusionWpfDropdownTest
{
    public class Market
    {
        public string? Name { get; set; }
    }
}

ViewModel.cs:

using System.Collections.ObjectModel;
using Npgsql;


namespace SyncfusionWpfDropdownTest
{
    public class ViewModel
    {
        public ObservableCollection<Country> Countries { get; set; }
        public ObservableCollection<Market> Markets { get; set; }


        public ViewModel()
        {
            Countries = new ObservableCollection<Country>();
            Markets = new ObservableCollection<Market>();
            LoadData();
        }


        private void LoadData()
        {
            NpgsqlConnection conn = new NpgsqlConnection("Host=192.168.1.2; Port=5432; Database=tradingapp; Username=vorlket; Password=Vcsokr.");
            conn.Open();
            string query = "SELECT DISTINCT Country FROM EconomicCalendar ORDER BY Country";
            NpgsqlCommand cmd = new NpgsqlCommand(query, conn);
            NpgsqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                Countries.Add(new Country
                {
                    Name = reader.GetString(0)
                });
            }


            NpgsqlConnection conn1 = new NpgsqlConnection("Host=192.168.1.2; Port=5432; Database=tradingapp; Username=vorlket; Password=Vcsokr.");
            conn1.Open();
            string query1 = "SELECT DISTINCT Name FROM Futures ORDER BY Name";
            NpgsqlCommand cmd1 = new NpgsqlCommand(query1, conn1);
            NpgsqlDataReader reader1 = cmd1.ExecuteReader();
            while (reader1.Read())
            {
                Markets.Add(new Market
                {
                    Name = reader1.GetString(0)
                });
            }
        }
    }
}

MainWindow.xaml:

<Window x:Class="SyncfusionWpfDropdownTest.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:local="clr-namespace:SyncfusionWpfDropdownTest" xmlns:syncfusion="http://schemas.syncfusion.com/wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.DataContext>
        <local:ViewModel/>
    </Window.DataContext>
    <Grid>
        <StackPanel HorizontalAlignment="Left" VerticalAlignment="Top">
            <syncfusion:DropDownButtonAdv IconHeight="1" IconWidth="1" SizeMode="Normal" x:Name="CountryButton" Label="Country">
                <syncfusion:DropDownMenuGroup ScrollBarVisibility="Visible" ItemsSource="{Binding Countries}">
                    <syncfusion:DropDownMenuGroup.ItemTemplate>
                        <DataTemplate>
                            <syncfusion:DropDownMenuItem Header="{Binding Name}" HorizontalAlignment="Left">
                            </syncfusion:DropDownMenuItem>
                        </DataTemplate>
                    </syncfusion:DropDownMenuGroup.ItemTemplate>
                </syncfusion:DropDownMenuGroup>
            </syncfusion:DropDownButtonAdv>
        </StackPanel>
        <StackPanel HorizontalAlignment="Center" VerticalAlignment="Top">
            <syncfusion:DropDownButtonAdv IconHeight="1" IconWidth="1" SizeMode="Normal" x:Name="MarketButton" Label="Market">
                <syncfusion:DropDownMenuGroup ScrollBarVisibility="Visible" ItemsSource="{Binding Markets}">
                    <syncfusion:DropDownMenuGroup.ItemTemplate>
                        <DataTemplate>
                            <syncfusion:DropDownMenuItem Header="{Binding Name}" HorizontalAlignment="Left">
                            </syncfusion:DropDownMenuItem>
                        </DataTemplate>
                    </syncfusion:DropDownMenuGroup.ItemTemplate>
                </syncfusion:DropDownMenuGroup>
            </syncfusion:DropDownButtonAdv>
        </StackPanel>
    </Grid>
</Window>

MainWindow.xaml.cs:

using System.Windows;


namespace SyncfusionWpfDropdownTest
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
    }
}

6 Replies 1 reply marked as answer

KJ Kook Jin Noh June 28, 2024 03:01 AM UTC

Hi Syncfusion team,

Just a reminder for help as this might have fallen off the radar.

Thanks.



PV Priyanka Vijayasankar Syncfusion Team June 28, 2024 03:51 PM UTC

Hi Kook Jin Noh,


We have checked your query and prepared a sample to meet the requirement as a workaround solution. Please find the sample below.

If we misunderstood the requirement, please provide an update. It will help us to provide a solution promptly.


Regards,

Priyanka Vijayasankar


Attachment: DropDownButtonAdv_a9be8f27.zip


KJ Kook Jin Noh replied to Priyanka Vijayasankar June 29, 2024 11:46 AM UTC

Hi Priyanka,

Thanks for your time helping me. I had a look at your codes and it doesn't seem to address my question. To begin answering my question in a right direction, answers to the following would help:

  1. How do you change the value of a dropdown when an item is clicked?
  2.  How do you assign this value to a variable in MainWindow.xaml.cs in DropDownMenuItem_xxxClick so that it can be used in a SQL query in ViewModel.cs?
  3. How do you use the assigned variable in 2 to make a query?
Thanks.


PV Priyanka Vijayasankar Syncfusion Team replied to Kook Jin Noh July 3, 2024 05:00 PM UTC

Hi Kook Jin Noh,

 

Query 1: In the DropDownMenuItem click event, set the selected header value to the DropDownButtonAdv instance's label property. Please refer to the attached screenshot in Query 2 solution.

 

Query 2: Create a new property in ViewModel.cs and set its value in the DropDownMenuItem click event. This value can be used in an SQL query. Please refer to the attached screenshot below,

DropDownButton_Query1&Query2.png

 

Query 3: In the setter property, the selected value is received and used to make a query by calling the new method. Please refer to the attached screenshot below,

DropDownButton_Query 3.png

 

We have attached a modified sample related to these queries. Please refer to the attached sample for your reference. Please let us know if you need any further assistance.

 

Regards,

Priyanka Vijayasankar


Attachment: DropDownButtonAdv_abb99e8.zip

Marked as answer

KJ Kook Jin Noh replied to Priyanka Vijayasankar July 4, 2024 03:04 AM UTC

Hi Priyanka, thanks for the help, appreciate it. Could you please help me working on these warn

Image_1170_1720062207680

Image_1585_1720062230782

Image_3668_1720062257618



PV Priyanka Vijayasankar Syncfusion Team replied to Kook Jin Noh July 4, 2024 04:11 PM UTC

Hi Kook Jin Noh,

 

To resolve possible null reference warnings, it is important to handle nullable values using the null-conditional (?.) and null-coalescing (??) operators. Additionally, ensure that non-nullable fields are initialized with default values where necessary. For further guidance on resolving null reference issues, please refer to the attached screenshot and visit the following link:


DropDown_PossibleNull.png

 DropDown_NullableField.png


https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/nullable-warnings#nonnullable-reference-not-initialized

 

Regards,

Priyanka Vijayasankar


Loader.
Up arrow icon