?
Solved

WPF binding

Posted on 2014-10-25
2
Medium Priority
?
132 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 2000 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

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

765 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