1

I am Having a Itemsource and from which i bind the values in the datagrid

    <DataGridTextColumn Header="Message" Binding="{Binding Path=message}"/>
    <DataGridTextColumn Header=" Logger" Binding="{Binding Path=logger}"/>
    <DataGridTextColumn Header="Level" Binding="{Binding Path=level}" />

I have to bind the header text as well with a Dictionary of keys

    Dictionary<String, object>.KeyCollection keyslist = dict1.Keys;

with this dictionary i have to bind the Header text.

is it possible to have two itemsource for a Datagrid ??

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
Raghu
  • 313
  • 2
  • 7
  • 19
  • i think you should explain further what you want. What should be DataGrid's ItemsSource and what shoulb be the header's Itemsource? – iltzortz Jan 21 '13 at 11:51
  • Datagrid has a IEnumerable collection(Say Result) as a Itemsource and when i bind it directly say this.datagrid.Itemsource=Result it gets binded automatically in the datagrid provided Autogenerate columns =True but i need to format the columns headers and columns so Autogeneratecolumns =true can not serve my need and now i have a list of column headers in the dictionary "Keyslist"......how to bind the keyslist to column header ?? or how can i get the column headers from "Result" – Raghu Jan 21 '13 at 12:22
  • So what is worng with your code ? Do you need something else than Header="Level" etc.? – iltzortz Jan 21 '13 at 12:36
  • no i dont want hardcoded Header like what i have done rather i need a header binded from a Itemsource Keyslist is the list of headernames extracted from Result so nothing wrong with the code but no hardcoding – Raghu Jan 21 '13 at 12:39
  • I get this feeling, you might better off doing this sort of thing with code-behind, but a xaml way of doing things is posted for you to explore – Maverik Jan 21 '13 at 18:25
  • code-behind is much better for me to do it .... – Raghu Jan 22 '13 at 03:56

1 Answers1

0

The short answer to your question is: Yes, its possible. While this is a messy way of doing things, and I would not recommend doing it this way (unless you really have to, since I don't know your context details), here's the idea:

  1. First problem is, Header binding seems to be broken somewhat. I was unable to bind to it through regular means except through Source={x:Static} to define an alternate datacontext.

  2. Binding to a collection isn't possible in a Header binding, so you'll need to give it a scaler value OR write a converter that takes a parameter in and looks it up in your dictionary to return the real value.

And here's the sample code show how we did it to test the binding (without converter):

XAML

<Window x:Name="window" x:Class="WpfDataGridMisc.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="214" DataContext="{Binding Source={x:Static wpfDataGridMisc:PersonsViewModel.Instance}}"
        xmlns:wpfDataGridMisc="clr-namespace:WpfDataGridMisc">
    <DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Persons}">
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding FirstName}" Header="{Binding Source={x:Static wpfDataGridMisc:PersonsViewModel.Instance}, Path=Header1}" />
        </DataGrid.Columns>
    </DataGrid>
</Window>

PersonsViewModel

using System;
using System.Collections.ObjectModel;

namespace WpfDataGridMisc
{
    public class PersonsViewModel
    {
        private static readonly PersonsViewModel Current = new PersonsViewModel();

        public static PersonsViewModel Instance { get { return Current; } }

        private PersonsViewModel()
        {
            Persons = new ObservableCollection<Person>
                {
                    new Person {FirstName = "Thomas", LastName = "Newman", Date = DateTime.Now},
                    new Person {FirstName = "Dave", LastName = "Smith", Date = DateTime.Now},
                };
            Header1 = "Header 1";
        }

        public ObservableCollection<Person> Persons { get; private set; }

        public string Header1 { get; set; }
    }
}

Person class is a standard poco deducable from Person in code above.

Credit: Thanks to Johan Larsson for the bulk of code. He was working on this solution and I was just helping but he felt I should post the answer as x:Static was my idea.

Community
  • 1
  • 1
Maverik
  • 5,619
  • 35
  • 48