Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Sadly any <code>DataGridColumn</code> hosted under <code>DataGrid.Columns</code> is not part of <code>Visual</code> tree and therefore not connected to the data context of the datagrid. So bindings do not work with their properties such as <code>Visibility</code> or <code>Header</code> etc (although these properties are valid dependency properties!). </p> <p>Now you may wonderhow is that possible? Isn't their <code>Binding</code> property supposed to be bound to the data context? Well it simply is a hack. The binding does not really work. It is actually the datagrid cells that <strong>copy</strong> / <strong>clone</strong> this binding object and use it for displaying their own contents!</p> <p>So now back to solving your issue, I assume that <code>HeaderItems</code> is a property of the object that is set as the <code>DataContext</code> of your parent View. We <em>can</em> connect the <code>DataContext</code> of the view to any <code>DataGridColumn</code> via something we call a <strong><code>ProxyElement</code></strong>.</p> <p>The example below illustrates how to connect a logical child such as <code>ContextMenu</code> or <code>DataGridColumn</code> to the parent View's <code>DataContext</code></p> <pre><code> &lt;Window x:Class="WpfApplicationMultiThreading.Window5" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:vb="http://schemas.microsoft.com/wpf/2008/toolkit" Title="Window5" Height="300" Width="300" &gt; &lt;Grid x:Name="MyGrid"&gt; &lt;Grid.Resources&gt; &lt;FrameworkElement x:Key="ProxyElement" DataContext="{Binding}"/&gt; &lt;/Grid.Resources&gt; &lt;Grid.DataContext&gt; &lt;TextBlock Text="Text Column Header" Tag="Tag Columne Header"/&gt; &lt;/Grid.DataContext&gt; &lt;ContentControl Visibility="Collapsed" Content="{StaticResource ProxyElement}"/&gt; &lt;vb:DataGrid AutoGenerateColumns="False" x:Name="MyDataGrid"&gt; &lt;vb:DataGrid.ItemsSource&gt; &lt;x:Array Type="{x:Type TextBlock}"&gt; &lt;TextBlock Text="1" Tag="1.1"/&gt; &lt;TextBlock Text="2" Tag="1.2"/&gt; &lt;TextBlock Text="3" Tag="2.1"/&gt; &lt;TextBlock Text="4" Tag="2.2"/&gt; &lt;/x:Array&gt; &lt;/vb:DataGrid.ItemsSource&gt; &lt;vb:DataGrid.Columns&gt; &lt;vb:DataGridTextColumn Header="{Binding DataContext.Text, Source={StaticResource ProxyElement}}" Binding="{Binding Text}"/&gt; &lt;vb:DataGridTextColumn Header="{Binding DataContext.Tag, Source={StaticResource ProxyElement}}" Binding="{Binding Tag}"/&gt; &lt;/vb:DataGrid.Columns&gt; &lt;/vb:DataGrid&gt; &lt;/Grid&gt; &lt;/Window&gt; </code></pre> <p>The view above encountered the same binding error that you have found if I did not have implemented the ProxyElement hack. The ProxyElement is any FrameworkElement that <strong>steals</strong> the <code>DataContext</code> from the main View and offers it to the logical child such as <code>ContextMenu</code> or <code>DataGridColumn</code>. For that it must be hosted as a <code>Content</code> into an invisible <code>ContentControl</code> which is under the same View.</p> <p>I hope this guides you in correct direction.</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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