Question

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.

Was it helpful?

Solution

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
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top