WPF 中的数据网格绑定

发布于 2024-11-04 01:21:41 字数 969 浏览 1 评论 0原文

我知道已经有人问过这个问题,但我已经完成了开发人员建议的几乎所有事情。

<DataGrid x:Name="Imported" VerticalAlignment="Top"
          DataContext="{Binding Source=list}"
          AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Path=Date}"/>
    </DataGrid.Columns>
</DataGrid>

我试图在模式对话框中显示这一点,并在模式对话框的构造函数中填充许可证列表。 但 DataGrid 内仍然没有填充任何内容。

构造函数代码:

public diagboxclass()
{
    List<object> list = new List<object>();
    list = GetObjectList();
}
public class object
{
    string id;
    DateTime date;
    public string ID
    {
        get { return id; }
        set { id = value; }
    }
    public DateTime Date
    {
        get { return date; }
        set { date = value; }
    }
}

你们认为与对象列表有关吗?

I know this has been asked already but I have done almost everything what is suggested by developers.

<DataGrid x:Name="Imported" VerticalAlignment="Top"
          DataContext="{Binding Source=list}"
          AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding Path=ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Path=Date}"/>
    </DataGrid.Columns>
</DataGrid>

I am trying to show this in modal dialog box and populating the license list in the constructor of the modal dialog box.
But still nothing is getting populated inside the DataGrid.

Constructor code:

public diagboxclass()
{
    List<object> list = new List<object>();
    list = GetObjectList();
}
public class object
{
    string id;
    DateTime date;
    public string ID
    {
        get { return id; }
        set { id = value; }
    }
    public DateTime Date
    {
        get { return date; }
        set { date = value; }
    }
}

Do you guys think something to do with the object list?

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

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

发布评论

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

评论(3

入画浅相思 2024-11-11 01:21:41

请不要使用object作为类名:

public class MyObject //better to choose an appropriate name
{
    string id;
    DateTime date;
    public string ID
    {
       get { return id; }
       set { id = value; }
    }
    public DateTime Date
    {
       get { return date; }
       set { date = value; }
    }
}

您应该为此类实现INotifyPropertyChanged,当然在属性设置器上调用它。否则更改不会反映在您的用户界面中。

您的 Viewmodel 类/对话框类应该具有 MyObject 列表的 PropertyObservableCollection 是正确的方法:

public ObservableCollection<MyObject> MyList
{
     get...
     set...
}

xaml 中,您应该将 Itemssource 设置为 MyObject 集合>。 (Datacontext 必须是您的对话框类!)

<DataGrid ItemsSource="{Binding Source=MyList}"  AutoGenerateColumns="False">
   <DataGrid.Columns>                
     <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
     <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
   </DataGrid.Columns>
</DataGrid>

PLEASE do not use object as a class name:

public class MyObject //better to choose an appropriate name
{
    string id;
    DateTime date;
    public string ID
    {
       get { return id; }
       set { id = value; }
    }
    public DateTime Date
    {
       get { return date; }
       set { date = value; }
    }
}

You should implement INotifyPropertyChanged for this class and of course call it on the Property setter. Otherwise changes are not reflected in your ui.

Your Viewmodel class/ dialogbox class should have a Property of your MyObject list. ObservableCollection<MyObject> is the way to go:

public ObservableCollection<MyObject> MyList
{
     get...
     set...
}

In your xaml you should set the Itemssource to your collection of MyObject. (the Datacontext have to be your dialogbox class!)

<DataGrid ItemsSource="{Binding Source=MyList}"  AutoGenerateColumns="False">
   <DataGrid.Columns>                
     <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
     <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
   </DataGrid.Columns>
</DataGrid>
つ低調成傷 2024-11-11 01:21:41

如果没有看到所述对象列表,我相信您应该绑定到 DataGridItemsSource 属性,而不是它的 DataContext

<DataGrid x:Name="Imported" VerticalAlignment="Top"
          ItemsSource="{Binding Source=list}"
          AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
   </DataGrid.Columns>
</DataGrid>

(这假设包含 DataGrid 的元素 [UserControl 等] 的 DataContext 绑定到包含 list 集合的对象 DataGrid 派生自 ItemsControl,它依赖于其 ItemsSource 属性来定义。其行绑定到的集合因此,如果。 list 不是绑定到控件的 DataContext 的对象的属性,您可能需要同时设置 DataContext={Binding list} 和 < DataGrid 上的 code>ItemsSource={Binding list})。

Without seeing said object list, I believe you should be binding to the DataGrid's ItemsSource property, not its DataContext.

<DataGrid x:Name="Imported" VerticalAlignment="Top"
          ItemsSource="{Binding Source=list}"
          AutoGenerateColumns="False" CanUserResizeColumns="True">
    <DataGrid.Columns>                
        <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
        <DataGridTextColumn Header="Date" Binding="{Binding Date}"/>
   </DataGrid.Columns>
</DataGrid>

(This assumes that the element [UserControl, etc.] that contains the DataGrid has its DataContext bound to an object that contains the list collection. The DataGrid is derived from ItemsControl, which relies on its ItemsSource property to define the collection it binds its rows to. Hence, if list isn't a property of an object bound to your control's DataContext, you might need to set both DataContext={Binding list} and ItemsSource={Binding list} on the DataGrid).

她比我温柔 2024-11-11 01:21:41

尝试在后面的代码中执行此操作:

public diagboxclass()
{
    List<object> list = new List<object>();
    list = GetObjectList();
    Imported.ItemsSource = null;
    Imported.ItemsSource = list;
}

还要确保您的列表已有效填充,并且正如 @Blindmeis 提到的,切勿使用已在 C# 中给出函数的单词。

Try to do this in the behind code:

public diagboxclass()
{
    List<object> list = new List<object>();
    list = GetObjectList();
    Imported.ItemsSource = null;
    Imported.ItemsSource = list;
}

Also be sure your list is effectively populated and as mentioned by @Blindmeis, never use words that already are given a function in C#.

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