Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong><code>DataGridColumns</code> does not lies in same visual tree as that of <code>DataGrid</code></strong>, so it doesn't inherit <code>DataContext</code> from its parent. That's why you are not able to bind it using simple binding syntax. However, there are many workarounds to achieve that.</p> <p><em>I'll list two of them here:</em></p> <p><strong>First</strong> take advantage of <code>Freezable</code> class. <strong><em>Magic of Freezable objects is that it can inherit the DataContext even when they’re not in the visual or logical tree</em></strong>. You can read more about here <a href="http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/" rel="nofollow">Bind Data when DataContext is not inherited</a>.</p> <p>Add this class in your project:</p> <pre><code>public class BindingProxy : Freezable { #region Overrides of Freezable protected override Freezable CreateInstanceCore() { return new BindingProxy(); } #endregion public object Data { get { return (object)GetValue(DataProperty); } set { SetValue(DataProperty, value); } } public static readonly DependencyProperty DataProperty = DependencyProperty.Register("Data", typeof(object), typeof(BindingProxy), new UIPropertyMetadata(null)); } </code></pre> <p>XAML usage:</p> <pre><code>&lt;DataGrid&gt; &lt;DataGrid.Resources&gt; &lt;local:BindingProxy x:Key="proxy" Data="{Binding}" /&gt; &lt;/DataGrid.Resources&gt; &lt;DataGrid.Columns&gt; &lt;DataGridComboBoxColumn ItemsSource="{Binding Source={StaticResource proxy}}"/&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre> <hr> <p><strong>Second</strong>, if you are using WPF 4.0, you can take adavantage of <a href="http://msdn.microsoft.com/en-us/library/ee795380%28v=vs.110%29.aspx" rel="nofollow">x:Reference</a> which allows you to bind to element name even if it does not exist in Visual tree.</p> <p>Create TextBlock whose Visibility will be Collapsed and set its DataContext to personsList.</p> <pre><code>&lt;TextBlock Visibility="Collapsed" x:Name="txt"/&gt; </code></pre> <p>and in code behind</p> <pre><code>txt.DataContext = personList; </code></pre> <p>Now you can bind in DataGrid like this:</p> <pre><code>&lt;TextBlock Visibility="Collapsed" x:Name="txt"/&gt; &lt;DataGrid&gt; &lt;DataGrid.Columns&gt; &lt;DataGridComboBoxColumn ItemsSource="{Binding Source={x:Reference txt}}"/&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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