Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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:</p> <ol> <li><p>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.</p></li> <li><p>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.</p></li> </ol> <p>And here's the sample code show how <em>we</em> did it to test the binding (without converter):</p> <p><strong>XAML</strong></p> <pre><code>&lt;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"&gt; &lt;DataGrid AutoGenerateColumns="False" ItemsSource="{Binding Persons}"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Binding="{Binding FirstName}" Header="{Binding Source={x:Static wpfDataGridMisc:PersonsViewModel.Instance}, Path=Header1}" /&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/Window&gt; </code></pre> <p><strong>PersonsViewModel</strong></p> <pre><code>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&lt;Person&gt; { new Person {FirstName = "Thomas", LastName = "Newman", Date = DateTime.Now}, new Person {FirstName = "Dave", LastName = "Smith", Date = DateTime.Now}, }; Header1 = "Header 1"; } public ObservableCollection&lt;Person&gt; Persons { get; private set; } public string Header1 { get; set; } } } </code></pre> <p>Person class is a standard poco deducable from Person in code above.</p> <p>Credit: Thanks to <a href="https://stackoverflow.com/users/1069200/johan-larsson">Johan Larsson</a> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload