Próbuję powiązać List
z DataGrid
. Oto fragment kodu:Wiązanie DataGrid do listy w wpf
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);
}
}
kod za:
Persons persons = new Persons();
persons.Add(new Parson("New","Person");
dataGrid1.DataContext = persons;
XAML: jest, że wyświetlana jest pusta siatka
<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>
Rezultatem! Ktoś wie dlaczego?
XAML: < /my:DataGrid.Columns> my: DataGrid> –
Czy okno wyjściowe zawiera coś związanego z tym powiązaniem? – andyp
Twój kod działa i działa dla mnie. Gdzie ustawiasz DataContext siatki? Ustawiłem go w Window.Loaded. – ArildF