Solved

WPF binding

Posted on 2014-10-25
2
102 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
Comment Utility
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:
Tchuki earned 500 total points
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now