Populate ComboBox in another form based on another ComboBox using C# and VB.Net in WPF Application

Last Reply 3 months ago By dharmendr

Posted 3 months ago

I want to update selected item in combobox in xaml file from another xaml file in wpf application.

Is this possible?

I have two forms First form have Country combobox and another form have State combobox and I want to update data in form two from Form one.

That means If I select Country in Form one then update State in Form two.

Posted 3 months ago Modified on 3 months ago

Hi Waghuldey,

You can pass the selected combobox value to second page using the constructor.

Check this example. Now please take its reference and correct your code.

XAML

Country

<Window x:Class="Cascading_ComboBox_WPF.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:Cascading_ComboBox_WPF"
        mc:Ignorable="d" WindowStartupLocation="CenterScreen"
        Title="Country" Height="193" Width="216">
    <Grid>
        <Label Content="Country : " HorizontalAlignment="Left" Margin="8,58,0,0" VerticalAlignment="Top"/>
        <ComboBox Name="cmbCountries" HorizontalAlignment="Left" Margin="69,62,0,0" VerticalAlignment="Top" Width="100" SelectionChanged="CountryChanged"/>
    </Grid>
</Window>

State

<Window x:Class="Cascading_ComboBox_WPF.StateWindow"
        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:Cascading_ComboBox_WPF"
        mc:Ignorable="d" WindowStartupLocation="CenterScreen"
        Title="State" Height="193" Width="216">
    <Grid>
        <Label Content="State : " HorizontalAlignment="Left" Margin="8,58,0,0" VerticalAlignment="Top"/>
        <ComboBox Name="cmbStates" HorizontalAlignment="Left" Margin="69,62,0,0" VerticalAlignment="Top" Width="100"/>
    </Grid>
</Window>

Code

C#

Country

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        List<ListItem> countries = new List<ListItem>()
        {
            new ListItem(){ Id=1, Name="USA"},
            new ListItem(){ Id=2, Name="India"},
            new ListItem(){ Id=3, Name="Canada"}
        };
        cmbCountries.ItemsSource = countries;
        cmbCountries.DisplayMemberPath = "Name";
        cmbCountries.SelectedValuePath = "Id";
    }

    private void CountryChanged(object sender, SelectionChangedEventArgs e)
    {
        int countryId = Convert.ToInt32(cmbCountries.SelectedValue);
        StateWindow stateWindow = new StateWindow(countryId);
        stateWindow.Show();
    }

    public class ListItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

State

public partial class StateWindow : Window
{
    public StateWindow()
    {
        InitializeComponent();
    }
    public StateWindow(int countryId)
    {
        InitializeComponent();
        BindStates(countryId);
    }

    private void BindStates(int countryId)
    {
        List<ListItem> states = new List<ListItem>();
        if (countryId == 1)
        {
            states = new List<ListItem>()
            {
                new ListItem(){ Id=1, Name="Alabama"},
                new ListItem(){ Id=2, Name="Arizona"},
                new ListItem(){ Id=3, Name="Alaska"}
            };
        }
        else if (countryId == 2)
        {
            states = new List<ListItem>()
            {
                new ListItem(){ Id=1, Name="Maharashtra"},
                new ListItem(){ Id=2, Name="Gujarat"},
                new ListItem(){ Id=3, Name="Goa"}
            };
        }
        else if (countryId == 3)
        {
            states = new List<ListItem>()
            {
                new ListItem(){ Id=1, Name="Ontario"},
                new ListItem(){ Id=2, Name="Quebec"},
                new ListItem(){ Id=3, Name="Manitoba"}
            };
        }
        cmbStates.ItemsSource = states;
        cmbStates.DisplayMemberPath = "Name";
        cmbStates.SelectedValuePath = "Id";
    }

    public class ListItem
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

VB.Net

Country

Class MainWindow
    Public Sub New()
        InitializeComponent()
        Dim countries As List(Of ListItem) = New List(Of ListItem)() From {
            New ListItem() With {.Id = 1, .Name = "USA"},
            New ListItem() With {.Id = 2, .Name = "India"},
            New ListItem() With {.Id = 3, .Name = "Canada"}
        }
        cmbCountries.ItemsSource = countries
        cmbCountries.DisplayMemberPath = "Name"
        cmbCountries.SelectedValuePath = "Id"
    End Sub

    Private Sub CountryChanged(ByVal sender As Object, ByVal e As SelectionChangedEventArgs)
        Dim countryId As Integer = Convert.ToInt32(cmbCountries.SelectedValue)
        Dim stateWindow As StateWindow = New StateWindow(countryId)
        stateWindow.Show()
    End Sub

    Public Class ListItem
        Public Property Id As Integer
        Public Property Name As String
    End Class
End Class

State

Public Class StateWindow
    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub New(ByVal countryId As Int32)
        InitializeComponent()
        CountryChanged(countryId)
    End Sub

    Private Sub CountryChanged(ByVal countryId As Int32)
        Dim states As List(Of ListItem) = New List(Of ListItem)()
        If countryId = 1 Then
            states = New List(Of ListItem)() From {
                New ListItem() With {.Id = 1, .Name = "Alabama"},
                New ListItem() With {.Id = 2, .Name = "Arizona"},
                New ListItem() With {.Id = 3, .Name = "Alaska"}
            }
        ElseIf countryId = 2 Then
            states = New List(Of ListItem)() From {
                New ListItem() With {.Id = 1, .Name = "Maharashtra"},
                New ListItem() With {.Id = 2, .Name = "Gujarat"},
                New ListItem() With {.Id = 3, .Name = "Goa"}
            }
        ElseIf countryId = 3 Then
            states = New List(Of ListItem)() From {
                New ListItem() With {.Id = 1, .Name = "Ontario"},
                New ListItem() With {.Id = 2, .Name = "Quebec"},
                New ListItem() With {.Id = 3, .Name = "Manitoba"}
            }
        End If

        cmbStates.ItemsSource = states
        cmbStates.DisplayMemberPath = "Name"
        cmbStates.SelectedValuePath = "Id"
    End Sub

    Public Class ListItem
        Public Property Id As Integer
        Public Property Name As String
    End Class
End Class

Screenshot