views:

431

answers:

1

Hi .. Is there any way to dynamically add a button control(along with column name) to WPFDataGrid column,??

By clicking on header button,pop-up will open .

this button generation is dynamic one ,which will be decided from code-behind, for some column headers need to add,for some not needed to add.

A: 

Maybe with a DataTemplate selector? Something like this:

XAML:

<ListView ItemsSource="{Binding}">
    <ListView.View>
     <GridView>
      <GridViewColumn>
       <GridViewColumn.HeaderTemplateSelector>
        <local:MyColumnHeaderSelector />
       </GridViewColumn.HeaderTemplateSelector>
      </GridViewColumn>
     </GridView>
    </ListView.View>
</ListView>

C#:

public class MyColumnHeaderSelector : DataTemplateSelector
{
 public override DataTemplate SelectTemplate(object item, DependencyObject container)
 {
  if(yourCondition == true)
  {
   return (DataTemplate)App.Current.MainWindow.FindResource("ColumnTemplateWithButton"); // this DataTemplate is defined in the resources of your window
  }
  else
  {
   return (DataTemplate)App.Current.MainWindow.FindResource("ColumnTemplateWithoutButton"); // this DataTemplate is defined in the resources of your window
  }
 }
}
Carlo