English 中文(简体)
装饰数据
原标题:Binding DataGrid to List in wpf

I m trying to bind a List to a DataGrid. Here is the code snippet:

public class Parson
{
    public string LastName { get; set; }
    public string FirstName { get; set; }

    public Parson(string lastName, string firstName)
    {
        LastName = lastName;
        FirstName = firstName;
    }
}

public class Persons : List<Parson>
{
    // Parameterless constructor      
    public Persons()
    {
    }
    public new void Add(Person parson)
    {
        base.Add(parson);
    }
}  

后面的法典:

Persons persons = new Persons();
persons.Add(new Parson("New","Person");
dataGrid1.DataContext = persons;

xaml:

<my:DataGrid Name="dataGrid1" 
             xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit" 
             CanUserAddRows="True" 
             ItemsSource="{Binding}" 
             AutoGenerateColumns="False">
    <my:DataGrid.Columns>
        <my:DataGridTextColumn Header="First Name" Binding="{Binding Path=FirstName}"/>
        <my:DataGridTextColumn Header="Last Name" Binding="{Binding Path=LastName}"/>
    </my:DataGrid.Columns>
</my:DataGrid>

The result is that an empty grid is displayed! Anyone know why?

问题回答

履历

(说明:这一答案在“NET Framework v4.0”中得到证实:

简单、四部分的行动......

  • first, on your DataGrid control in the XAML mark-up I recommend you set AutoGenerateColumns="False" (you should be able to do this manually better than they can do it automatically)
  • second, on the same control set ItemsSource="{Binding}", which informs the grid it will be getting it s column and row data from a source not defined at design-time.
  • third, construct the columns to look how you want them to look and for each bind to the column data with something like Binding="{Binding Path=UniqueID}". Note that the value after Path is interpolated so mind things like case-sensitivity. For the List<> you are targetting the value will presumably be one of the property-names from objects in your target List<>. Rinse and repeat as many times as needed for each column in your DataGrid.
  • fourth, in your form code-behind, within the constructor or during form load or wherever it best suits your needs, set the DataContext of your grid. This should take a form similar to {gridControlName}.DataContext = {target-List<>}

当表格装上电网时,该表应自动填入其清单和设计中的物体;

说明项目内容而不是数据内容,删除<代码> 页: 1 这可能是徒劳的。

Edit:

我刚刚检查了一些代码,写道采用了相同的数据控制(WPF工具kit),事实上,我确实制定了项目资源,而不是数据内容。 如果需要一个例子,让我知道。

在你的数据中:

ItemsSource="{Binding Path=.}"

这对我来说是 worked的。





相关问题
WPF convert 2d mouse click into 3d space

I have several geometry meshes in my Viewport3D, these have bounds of (w:1800, h:500, d:25). When a user clicks in the middle of the mesh, I want the Point3D of (900, 500, 25)... How can I achieve ...

Editing a xaml icons or images

Is it possible to edit a xaml icons or images in the expression design or using other tools? Is it possible to import a xaml images (that e.g you have exported) in the expression designer for editing?...

WPF: writing smoke tests using ViewModels

I am considering to write smoke tests for our WPF application. The question that I am faced is: should we use UI automation( or some other technology that creates a UI script), or is it good enough to ...

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

How do WPF Markup Extensions raise compile errors?

Certain markup extensions raise compile errors. For example StaticExtension (x:Static) raises a compile error if the referenced class cannot be found. Anyone know the mechanism for this? Is it baked ...

WPF design-time context menu

I am trying to create a custom wpf control, I m wondering how I can add some design-time features. I ve googled and can t seem to get to my goal. So here s my simple question, how can I add an entry ...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签