Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Edit</strong>: this a new answer; I left the old answer at the bottom.</p> <p>If you set Path=. in the CommandParameter you get the ContentPresenter as the object in the Command. At this point I still could not reference the Uid or Name of the Column. Thus I created DataGridTextColumnWithTag that inherits from DataGridTextColumn and I added a DependencyProperty called Tag. Now in OnDrilldownHeaderClicked I can determine the column clicked on in the Grid.</p> <p>Here is the DataTemplate in XAML:</p> <pre><code>&lt;Window.Resources&gt; &lt;c:CommandReference x:Key="DrilldownHeaderClickedReference" Command="{Binding DrilldownHeaderClicked}" /&gt; &lt;DataTemplate x:Key="ClickableHeaderTemplate"&gt; &lt;Button Content="{Binding}" Background="Transparent" Command="{StaticResource DrilldownHeaderClickedReference}" CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=.}" &gt; &lt;/Button&gt; &lt;/DataTemplate&gt; &lt;/Window.Resources&gt; </code></pre> <p>Here is the definition of the DataGrid Column in XAML:</p> <pre><code>&lt;Helpers:DataGridTextColumnWithTag Header="Extra Name" Tag="234" Width="SizeToCells" Binding="{Binding FinalName}" FontSize="20" HeaderTemplate="{StaticResource ResourceKey=ClickableHeaderTemplate}"&gt; &lt;/Helpers:DataGridTextColumnWithTag&gt; </code></pre> <p>Here is the new DataGrid column:</p> <pre><code>public class DataGridTextColumnWithTag : DataGridTextColumn { public DataGridTextColumnWithTag() : base() { } public string Tag { get { return (string)this.GetValue(TagProperty); } set { this.SetValue(TagProperty, value); } } public static readonly DependencyProperty TagProperty = DependencyProperty.Register( "Tag", typeof(string), typeof(DataGridTextColumnWithTag), new PropertyMetadata(string.Empty)); } </code></pre> <p>Here is Command Binding used to find the column clicked on:</p> <pre><code>public ICommand DrilldownHeaderClicked { get; set; } private void OnDrilldownHeaderClicked(object obj) { ContentPresenter cp = obj as ContentPresenter; if (cp != null) { DataGridColumnHeader tp = cp.TemplatedParent as DataGridColumnHeader; if ( tp != null ) { DataGridTextColumnWithTag column = tp.Column as DataGridTextColumnWithTag; if ( column != null ) { string tag = column.Tag; } } } } </code></pre> <p><strong>Note</strong>: this is the old answer. This code gets the column header, but is brittle if/when the Column name changes.</p> <p>The following DataTemplate gets me the Header of the column.<br> I then use the header to figure out the column.<br> Hopefully this gets you started.<br> If you can extend this solution please post your answer.</p> <pre><code>&lt;UserControl.Resources&gt; &lt;c:CommandReference x:Key="DrilldownHeaderClickedReference" Command="{Binding DrilldownHeaderClicked}" /&gt; &lt;DataTemplate x:Key="ClickableHeaderTemplate"&gt; &lt;Button x:Name="btn" Content="{Binding}" Background="Transparent" Command="{StaticResource DrilldownHeaderClickedReference}" CommandParameter="{Binding}" &gt; &lt;/Button&gt; &lt;/DataTemplate&gt; &lt;/UserControl.Resources&gt; </code></pre>
 

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