WPF ItemsSource 绑定

5

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

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

我的数据网格在一个分组中,其中数据上下文设置为主窗口:

<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服务。
有人知道我在这里做错了什么吗?

我认为在DataGrid中的DataContext =“{Binding}”是多余的,因为通常情况下DataContext会被继承。 - H.B.
@H.B. 谢谢,我已经清理了。但是没有什么变化(你也没指望会有)。可能是因为我尝试了多种方法的结果。 - Redbaran
3个回答

3

我认为你不需要在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";

注:该内容涉及IT技术。

你能解释一下什么是设计师吗?你是在指绑定吗?如果是的话,那么是的,请将项源设置为集合,然后你就可以开始了。 - rlcrews

2

这可能是设计师为了避免频繁编译而采用的一些技巧(例如跳过代码后面的构造函数)所导致的结果。尝试将你的集合移动到一个单独的类中,并将该类的实例用作你的DataContext(类似于MVVM ViewModel)。另一个类应该能够正常初始化并向设计师提供绑定属性。


1

你尝试过在GroupBox和DataGrid中不使用DataContext标签吗?

编辑

像这样:

<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>

我不确定你的意思,请更具体一些。现在XAML标记是由Visual Studio生成的。 - Redbaran
Daniel,那不起作用。没有设置数据上下文,我的属性获取就不再被调用,也不允许我使用属性窗口编辑列。 - Redbaran

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接