0

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.

1 Answer 1

1

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
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.