0

有没有办法将按钮控件(连同列名)动态添加到 WPFDataGrid 列,?

通过单击标题按钮,弹出窗口将打开。

此按钮生成是动态的,由代码隐藏决定,有些列标题需要添加,有些不需要添加。

4

1 回答 1

1

也许使用 DataTemplate 选择器?像这样的东西:

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
        }
    }
}
于 2009-10-07T15:20:30.140 回答