我很难在此数据网格视图中显示任何数据。我在其他一些 StackOverflow 论坛帖子中遵循了一些建议,但没有任何运气让内容出现。
<DataGrid
x:Name="DataGridEmployees"
DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
ItemsSource="{Binding GridView}"
AutoGenerateColumns="True"
Loaded="dataGrid1_Loaded"
Margin="0,2,0,-2" Grid.ColumnSpan="2">
<DataGrid.Columns>
<DataGridTextColumn Header="EmployeeId" Width="175" Binding="{Binding Id}"></DataGridTextColumn>
<DataGridTextColumn Header="Title" Width="175" Binding="{Binding Title}"></DataGridTextColumn>
<DataGridTextColumn Header="WorkStatus" Width="175" Binding="{Binding WorkStatus}"></DataGridTextColumn>
<DataGridTextColumn Header="FullName" Width="175" Binding="{Binding FullName}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
这是从 xaml.cs 文件中的单独窗口触发的单击事件(?这会导致任何问题吗?)
public partial class MainMenu : Window
{
WpfSampleEntities2 _context = new WpfSampleEntities2();
public MainMenu()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
EmployeeDetails ed = new EmployeeDetails();
ed.DataContext = ed.DomainEmployees;
Binding bin = new Binding("GridView");
bin.Source = ed.DomainEmployees;
foreach (var item in ed.DomainEmployees)
{
bin.Path.PathParameters.Add(item);
}
ed.Show();
}
}
这是EmployeeDetails.cs
类/虚拟机:
[TypeConverter(typeof(DataGridTextColumn))]
public class MVVMEmployee : Employee
{
public int Id { get; set; }
public string FullName { get; set; }
public string Title { get; set; }
public string WorkStatus { get; set; }
public MVVMEmployee() { }
public MVVMEmployee(int id, string fullName, string title, string workStatus)
{
this.Id = id;
this.FullName = fullName;
this.Title = title;
this.WorkStatus = workStatus;
}
}
我也尝试过XAML
as:
<DataGrid
x:Name="DataGridEmployees"
DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
ItemsSource="{Binding GridView}"
AutoGenerateColumns="True"
Loaded="dataGrid1_Loaded"
Margin="0,2,0,-2" Grid.ColumnSpan="2">
<DataGrid.Columns>
<DataGridTextColumn Header="EmployeeId" Width="175" Binding="{Binding ElementName=Id}" ></DataGridTextColumn>
<DataGridTextColumn Header="Title" Width="175" Binding="{Binding ElementName=Title}"></DataGridTextColumn>
<DataGridTextColumn Header="WorkStatus" Width="175" Binding="{Binding ElementName=WorkStatus}"></DataGridTextColumn>
<DataGridTextColumn Header="FullName" Width="175" Binding="{Binding ElementName=FullName}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>