Solved

WPF binding

Posted on 2014-10-25
2
122 Views
Last Modified: 2014-11-18
Hi,

I have a list as below

-Branch
  Branchname = AB
  BranchCode = 01
  -Counters
     -Countername = AB-1
      Countercode = 101
      countername = AB-2
      countercode = 102
   Branchname = CD
   BranchCode = 02
     -Counter
         -Countername = CD-1
          Countercode = 201
          countername = CD-2
          countercode = 202

i have 2 listboxes in UI, first list box should load all the branches and the second list box should have the corresponding counters for the selected branch.

Can anyone help me in binding the nested listbox to 2 listbox.

Thanks,
Rajeeva
0
Comment
Question by:rajeeva_nagaraj
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

Expert Comment

by:apeter
ID: 40405377
You have to use a objectdataprovider to bind the xml to the listbox. You can bind the branches to the first listbox . When an item is selected in the first listbox, its corresponding counters can be loaded in second list box using the "selecteditem" context.

This url should give you more details.  http://msdn.microsoft.com/en-us/magazine/cc163299.aspx
0
 
LVL 14

Accepted Solution

by:
Vel Eous earned 500 total points
ID: 40406030
Try the following and see how it works for you.  Note that I made some assumptions regarding the structuring of your objects.

MainWindow.xaml
<Window x:Class="EE.Q_28544482.Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        DataContext="{Binding RelativeSource={RelativeSource Self}}"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <ComboBox Name="CbBranches" VerticalAlignment="Top" Margin="10" ItemsSource="{Binding Branches}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding BranchName}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
            </ComboBox>
        <ComboBox Name="CbCounters" VerticalAlignment="Top" Margin="10" ItemsSource="{Binding ElementName=CbBranches, Path=SelectedItem.Counters}">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding CounterName}" />
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>
    </StackPanel>
</Window>

Open in new window


MainWindow.xaml.cs
namespace EE.Q_28544482.Wpf
{
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;
    using EE.Q_28544482.Wpf.Annotations;

    public partial class MainWindow : INotifyPropertyChanged
    {
        private ObservableCollection<Branch> branches;

        public MainWindow()
        {
            this.branches = new ObservableCollection<Branch>
            {
                new Branch
                {
                    BranchName = "AB",
                    BrachCode = 01,
                    Counters = new List<Branch.Counter>
                    {
                        new Branch.Counter
                        {
                            CounterName = "AB-1",
                            CounterCode = 101
                        },
                        new Branch.Counter
                        {
                            CounterName = "AB-2",
                            CounterCode = 102
                        }
                    }
                },
                new Branch
                {
                    BranchName = "CD",
                    BrachCode = 02,
                    Counters = new List<Branch.Counter>
                    {
                        new Branch.Counter
                        {
                            CounterName = "CD-1",
                            CounterCode = 201
                        },
                        new Branch.Counter
                        {
                            CounterName = "CD-2",
                            CounterCode = 202
                        }
                    }
                }
            };
            this.InitializeComponent();
        }

        public ObservableCollection<Branch> Branches
        {
            get { return this.branches; }
            set
            {
                if (Equals(value, this.branches))
                {
                    return;
                }
                this.branches = value;
                this.OnPropertyChanged();
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        [NotifyPropertyChangedInvocator]
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class Branch
    {
        public string BranchName { get; set; }
        public int BrachCode { get; set; }
        public List<Counter> Counters { get; set; }

        public class Counter
        {
            public string CounterName { get; set; }
            public int CounterCode { get; set; }
        }
    }
}

Open in new window

0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question