Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 944347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T22:34:41+00:00 2026-05-15T22:34:41+00:00

I am trying to bind a dictionary’s key to a row of the grid

  • 0

I am trying to bind a dictionary’s key to a row of the grid in a listbox, and bind the dictionary’s value to another row of the grid. key’s type is Book, a class thati wrote and the value’s type is int.
i want to write the elements of the class and the integer value in grid.
Can you help me on this? I’m quite confused on determining the itemsSource and the data type to bind.
thanks for helping

Edit: i forgot to say that i am using c# – wpf. =)


I sent the dictionary as the itemsSource, and i specified the dictionary as type in objectdataprovider tag, and tried to send the value (int) by this code:

< TextBlock Text="{Binding Value, Mode=OneWay}" Grid.Row="1" Width="65" >

and the selecteditem was shown as [myNameSpace.Book, 4] instead of only 4.


BookListBox.ItemsSource = LibManager.Books;

this is what I wrote in Window.xaml.cs and Books is a BookList, where BookList is a type of dictionary< Book, int> .

and the xaml file:

< ListBox Height="571" HorizontalAlignment="Left" Margin="444,88,0,0"
          Name="BookListBox" VerticalAlignment="Top" Width="383" >
        < ListBox.Resources>
            <ObjectDataProvider x:Key="BookData"
                                ObjectType="{x:Type local:BookList}"/>
        </ListBox.Resources>
        < ListBox.ItemTemplate>
            < DataTemplate>
                < Border BorderThickness="2" BorderBrush="Black" Margin="5"
                         CornerRadius="5" Width="350" >
                    < Grid DataContext="{StaticResource BookData}" >
                        < Grid.ColumnDefinitions>
                            < ColumnDefinition/>                        
                        </Grid.ColumnDefinitions>
                        < Grid.RowDefinitions>
                            < RowDefinition/>
                            < RowDefinition/>
                        < /Grid.RowDefinitions>
                        < Label Content="count: " />
                        < TextBlock Text="{Binding Value, Mode=OneWay}"
                                    Grid.Row="1" Width="65"/>
                    < /Grid>
                < /Border>
            < /DataTemplate>
        < /ListBox.ItemTemplate>
    < /ListBox>

there is one more problem with my code– I cant see the listed items in listbox. I mean I could get the values by ListBox.SelectedItem but couldn’t see in the listbox. thus I can’t be sure if I could pass the integer value to where I want.

So I think I also need help for this problem first…
I write a label that I write by hand, and another label that should be filled by data binding in the same row but I can just see the first label, but I can reach the value in code behind.

  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T22:34:41+00:00Added an answer on May 15, 2026 at 10:34 pm

    Below code will show the following:

    1
    Book 1
    -------
    2
    Book 2
    -------
    3
    Book 3
    

    SelectedBookIndex will be set to the index of the selected book, at start the second book will be selected.

    XAML:

    <Window x:Class="TestDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="300" Width="300">
    
        <StackPanel>
            <ListBox 
                ItemsSource="{Binding Path=Books}"
                SelectedValuePath="Value"
                SelectedValue="{Binding Path=SelectedBookIndex}">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Border BorderBrush="Black" BorderThickness="2">
                            <StackPanel>
                                <TextBlock Text="{Binding Path=Value}" />
                                <TextBlock Text="{Binding Path=Key.Name}" />
                            </StackPanel>
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </StackPanel>
    </Window>
    

    Code behind:

    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Windows;
    
    namespace TestDemo
    {
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
    
                Books = new Dictionary<Book, int>();
                Books.Add(new Book() { Name = "Book 1"}, 1);
                Books.Add(new Book() { Name = "Book 2" }, 2);
                Books.Add(new Book() { Name = "Book 3" }, 3);
    
                SelectedBookIndex = 2;
    
                DataContext = this;
            }
    
            public Dictionary<Book, int> Books { get; set; }
    
            private int _selectedBookIndex;
            public int SelectedBookIndex
            {
                get { return _selectedBookIndex; }
                set
                {
                    _selectedBookIndex = value;
                    Debug.WriteLine("Selected Book Index=" + _selectedBookIndex);
                }
            }
        }
    
        public class Book
        {
            public string Name { get; set; }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to bind a dictionary as a DataSource to a ListBox. The
I have the following class that I want to bind it's dictionary's value to
I'm trying to Databind to my custom dictionary class. In formLoad, I can bind
I'm trying to bind to a Dictionary<Type,string> through xaml. The problem is, the indexer
I'm trying to bind KeyValuePair Elements from a Dictionary to a ItemsControl. My Dictionary
I'm trying to bind a list of data to a data grid, but can't
I am trying to bind Dictionary to a Chart, Below is the code IDictionary<double,
Im trying to bind a list of images to a Listbox, but all I
I am trying to bind several ListBoxs to a List. When a ListBox on
I want to bind a textBox's data to a Dictionary<string,string> entry. I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.