WPF 项目源绑定

发布于 2024-10-27 08:11:41 字数 1298 浏览 4 评论 0原文

我的 WPF 应用程序中有一个 Datagrid 控件,我试图将该控件绑定到主窗口类中的 ObservableCollection 属性。我尝试绑定的属性定义为:

private ObservableCollection<RequestResult> m_SentRequests = new ObservableCollection<RequestResult>();
public ObservableCollection<RequestResult> SentRequests { get { return m_SentRequests; } }

我的数据网格位于将数据上下文设置为 MainWindow 的组中:

<GroupBox Header="Results" Height="275" HorizontalAlignment="Stretch" Margin="0,305,0,0" Name="grpResults" VerticalAlignment="Top" Width="712" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}}">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="246" HorizontalAlignment="Stretch" Margin="6,6,6,0" Name="dgResults" VerticalAlignment="Top" ItemsSource="{Binding Path=SentRequests}" DataContext="{Binding}" IsSynchronizedWithCurrentItem="True" />
    </Grid>
</GroupBox>

我遇到的问题是在属性窗口中,在我选择 SentRequests 作为我的 ItemsSource 之后,我仍然无法选择“编辑属性绑定列”选项。我收到一个“您必须先设置 ItemsSource,然后才能执行此操作”对话框。选择“生成列”和“删除列”时出现相同的错误。就好像我没有在对话框的 ItemsSource 属性中设置任何内容。

我可以将 AutoGenerateColumns 设置为 true,但我看到我的数据已绑定(但是,不是与我想要显示的列绑定)。

我对 WPF 非常陌生,我只是将其编写为一个用于测试 Windows 服务的快速测试应用程序。

有人知道我在这里做错了什么吗?

I have a Datagrid control in my WPF application and I am trying to bind that control to an ObservableCollection property in my Main Window's class. The property I'm trying to bind to is defined as:

private ObservableCollection<RequestResult> m_SentRequests = new ObservableCollection<RequestResult>();
public ObservableCollection<RequestResult> SentRequests { get { return m_SentRequests; } }

My datagrid is in a group by which has the datacontext set to the MainWindow:

<GroupBox Header="Results" Height="275" HorizontalAlignment="Stretch" Margin="0,305,0,0" Name="grpResults" VerticalAlignment="Top" Width="712" DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=my:MainWindow, AncestorLevel=1}}">
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="246" HorizontalAlignment="Stretch" Margin="6,6,6,0" Name="dgResults" VerticalAlignment="Top" ItemsSource="{Binding Path=SentRequests}" DataContext="{Binding}" IsSynchronizedWithCurrentItem="True" />
    </Grid>
</GroupBox>

The problem that I'm having is that in the properties window, after I select SentRequests as my ItemsSource, I still can't select the "Edit Property-Bound Columns" option. I get a "You must set ItemsSource before you can perform this action" dialog. I get the same error when selecting "Generate Columns" and "Remove Columns". It's as if I haven't set anything in the ItemsSource property for my Dialog.

I can set AutoGenerateColumns to true though and I see my data get bound though (however, not with the columns I want to show).

I'm very new to WPF and I'm just writing this as a quick test app for testing a windows service.

Any one know what I'm doing wrong here?

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

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

发布评论

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

评论(3

再可℃爱ぅ一点好了 2024-11-03 08:11:41

我认为您不需要 itemSource 中的 Path 参数。您应该能够将绑定设置为 ItemsSource={Binding SentRequests}

您还可以在代码中绑定到网格项目源,例如,如果我创建一个虚拟集合:

    public class People
{

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Age { get; set; }
    public string address { get; set; }
    public string zip { get; set; }
}

然后填充它,

this.AllPeople = new ObservableCollection<People>();


        private void FillData()
    {
        People p1 = new People();
        p1.FirstName = "John";
        p1.LastName = "Doe";
        p1.Age = "24";
        p1.address = "123 Main Street";
        p1.zip = "11111";

        People p2 = new People();
        p2.FirstName = "Jane";
        p2.LastName = "Smith";
        p2.Age = "36";
        p2.address = "456 Water Street";
        p2.zip = "22222";

        People p3 = new People();
        p3.FirstName = "Larry";
        p3.LastName = "Williams";
        p3.Age = "24";
        p3.address = "785 Water Street";
        p3.zip = "33333";

        this.AllPeople.Add(p1);
        this.AllPeople.Add(p2);
        this.AllPeople.Add(p3);
    }

然后我可以在主页中设置项目源构造函数或方法为:

this.gridviewname.ItemsSource = "AllPeople";

I don't believe you need the Path parameter within your itemSource. You should be able to just set the binding as ItemsSource={Binding SentRequests}

You can also bind to the grid item source in code for example if I create a dummy collection:

    public class People
{

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Age { get; set; }
    public string address { get; set; }
    public string zip { get; set; }
}

and then populate it

this.AllPeople = new ObservableCollection<People>();


        private void FillData()
    {
        People p1 = new People();
        p1.FirstName = "John";
        p1.LastName = "Doe";
        p1.Age = "24";
        p1.address = "123 Main Street";
        p1.zip = "11111";

        People p2 = new People();
        p2.FirstName = "Jane";
        p2.LastName = "Smith";
        p2.Age = "36";
        p2.address = "456 Water Street";
        p2.zip = "22222";

        People p3 = new People();
        p3.FirstName = "Larry";
        p3.LastName = "Williams";
        p3.Age = "24";
        p3.address = "785 Water Street";
        p3.zip = "33333";

        this.AllPeople.Add(p1);
        this.AllPeople.Add(p2);
        this.AllPeople.Add(p3);
    }

I could then set the items source in the mainpage contsructor or method as:

this.gridviewname.ItemsSource = "AllPeople";

左岸枫 2024-11-03 08:11:41

这可能是设计者在不不断编译的情况下进行渲染的一些技巧(例如跳过代码隐藏构造函数)的结果。尝试将您的集合移至单独的类,并使用该类的实例作为您的 DataContext(如 MVVM ViewModel)。另一个类应该能够正常初始化并向设计者提供绑定属性。

This is probably a result of some of the trickery that the designer does to render without constantly compiling (like skipping code-behind constructors). Try moving your collection to a separate class and use an instance of that as your DataContext (like an MVVM ViewModel). The other class should be able to initialize normally and provide the bound property to the designer.

椵侞 2024-11-03 08:11:41

您是否尝试过不使用 DataContext 标签?在 GroupBox 和 DataGrid 中。

编辑

类似这样的内容:

<GroupBox Header="Results" Height="275" HorizontalAlignment="Stretch" >
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="246" HorizontalAlignment="Stretch"  Name="dgResults" VerticalAlignment="Top" ItemsSource="{Binding Path=SentRequests}" IsSynchronizedWithCurrentItem="True" />
    </Grid>
</GroupBox>

Have you tryed without the DataContext tags? Both in GroupBox and DataGrid.

EDIT

something like this:

<GroupBox Header="Results" Height="275" HorizontalAlignment="Stretch" >
    <Grid>
        <DataGrid AutoGenerateColumns="False" Height="246" HorizontalAlignment="Stretch"  Name="dgResults" VerticalAlignment="Top" ItemsSource="{Binding Path=SentRequests}" IsSynchronizedWithCurrentItem="True" />
    </Grid>
</GroupBox>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文