Solved

WPF binding

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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