Solved

WPF binding

Posted on 2014-10-25
2
111 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
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

791 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