如何将 WPFToolkit DataGrid 连接到代码隐藏中的搜索查询源

发布于 2024-08-14 07:33:56 字数 172 浏览 8 评论 0原文

我正在实现一个 WPFToolkit 数据网格,我想将它绑定到它的源。目前,我通过将查询结果链接到 ItemsSource 以编程方式在代码隐藏中执行此操作。但这仍然不够有效,因为我希望能够选择要显示的列并重命名标题,为此我需要绑定每个 datagridtextcolumn 部分。

我会很感激任何想法。先感谢您!

I am implementing a WPFToolkit datagrid and I want to bind it to it's source. Currently I do it programatically in code-behind through linking the query results to the ItemsSource . Still this is not effective enough, as I want to be able to select which columns to display and also to rename the headers, for which I need binding for each datagridtextcolumn sections.

I will appreciate any ideas. Thank you in advance!

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

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

发布评论

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

评论(1

单挑你×的.吻 2024-08-21 07:33:56

您还可以在代码隐藏中设置每列的绑定。

下面是一个示例 DataGrid,其中包含带有自定义标题文本的手动定义的列:

    <toolkit:DataGrid x:Name="dataGrid" AutoGenerateColumns="False">
        <toolkit:DataGrid.Columns>
            <toolkit:DataGridTextColumn x:Name="column1" Header="My Header" />
            <toolkit:DataGridTextColumn x:Name="column2" Header="Another Header" />
            <toolkit:DataGridTextColumn x:Name="column3" Header="Third Header" />
        </toolkit:DataGrid.Columns>
    </toolkit:DataGrid>

下面是一些隐藏代码,它将每列绑定到设置为 ItemsSource 的集合中的属性:

this.dataGrid.ItemsSource = this.testCollection; // replace with your collection
this.column1.Binding = new Binding("Property1"); // replace with your property names
this.column2.Binding = new Binding("Property2");
this.column3.Binding = new Binding("Property3");

您可以更改代码中的标题文本和可见性如果您愿意:

this.column1.Header = "New Header";
this.column2.Visibility = System.Windows.Visibility.Collapsed;

我只是使用一个简单的测试类 Prop 和一些字符串属性。

public class Prop
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }

    public Prop(string p1, string p2, string p3)
    {
        this.Property1 = p1;
        this.Property2 = p2;
        this.Property3 = p3;
    }
}

You can also set the bindings of each column in code-behind.

Here is a sample DataGrid with manually-defined columns with custom header text:

    <toolkit:DataGrid x:Name="dataGrid" AutoGenerateColumns="False">
        <toolkit:DataGrid.Columns>
            <toolkit:DataGridTextColumn x:Name="column1" Header="My Header" />
            <toolkit:DataGridTextColumn x:Name="column2" Header="Another Header" />
            <toolkit:DataGridTextColumn x:Name="column3" Header="Third Header" />
        </toolkit:DataGrid.Columns>
    </toolkit:DataGrid>

And here is some code-behind that binds each column to a property from the collection that is set to be the ItemsSource:

this.dataGrid.ItemsSource = this.testCollection; // replace with your collection
this.column1.Binding = new Binding("Property1"); // replace with your property names
this.column2.Binding = new Binding("Property2");
this.column3.Binding = new Binding("Property3");

You can change the header text and visibility in code if you like:

this.column1.Header = "New Header";
this.column2.Visibility = System.Windows.Visibility.Collapsed;

I'm just using a simple test class, Prop, with some string properties.

public class Prop
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
    public string Property3 { get; set; }

    public Prop(string p1, string p2, string p3)
    {
        this.Property1 = p1;
        this.Property2 = p2;
        this.Property3 = p3;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文