如何动态添加按钮到WPF Datagrid列?

发布于 2024-08-07 11:04:50 字数 118 浏览 3 评论 0原文

有没有办法动态地将按钮控件(以及列名称)添加到 WPFDataGrid 列?

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风吹过旳痕迹 2024-08-14 11:04:50

也许使用 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
        }
    }
}

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
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文