我很难在此数据网格视图中显示任何数据。我已经在其他一些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文件中从单独窗口触发的click事件(?这可能导致任何问题吗?)
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
class / vm:
[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
作为:
<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>
答案 0 :(得分:0)
myWindow w = new myWindow();
w.DataContext = myViewModel;
w.Show();
DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
OR
<Window x:Name="MyWindow" />
//DataGrid
{Binding DataContext, ElementName=MyWindow}
ItemsSource="{Binding MyViewModel.MyList}"
答案 1 :(得分:0)
我建议在Window中使用 ICollectionView 界面 或在我的模型视图类中更好
这是一个示例 MainWindow.xaml.cs 类,该类演示了如何通过隐藏代码将数据添加到此类接口Class中:
import pandas as pd
import math
def calc_distance(x):
A = [4, 0, 9] # replace this with your A values
return math.sqrt(((A[0]-x['R'])**2)+((A[1]-x['G'])**2)+((A[2]-x['B'])**2))
df = pd.read_csv('fuel_veg_cover.csv')
df['distance'] = df.apply(calc_distance, axis=1)
df = df.sort_values(by=['distance'])
df
MainWindow.xaml 中的绑定应如下所示:
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
namespace WpfAppTest
{
public partial class MainWindow : Window
{
public ICollectionView MyMVVMEmployeeList { get; private set; }
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
List<MVVMEmployee> list = new List<MVVMEmployee>();
list.Add(new MVVMEmployee(23, "foomaster", "dr", "busy"));
list.Add(new MVVMEmployee(42, "author", "mister", "dead"));
MyMVVMEmployeeList = CollectionViewSource.GetDefaultView(list);
//we call update gui //not needed when using modelview in pure mvvm
DataContext = this;
}
}
}
要使其完全可以在此处运行您的数据类 MVVMEmployee.cs ,而无需引用基类:
<Window x:Class="WpfAppTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfAppTest"
mc:Ignorable="d"
DataContext="MainWindow"
Title="MainWindow" Height="450" Width="800">
<!-- for mvvm pattern using the model for datacontext deps e.g. like -->
<!-- DataContext="{Binding Main,Source={StaticResource Locator}}"> -->
<Grid>
<StackPanel Orientation="Vertical">
<DataGrid x:Name="DataGridEmployees"
ItemsSource="{Binding MyMVVMEmployeeList }"
AutoGenerateColumns="False">
<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>
<Button Content="add" HorizontalAlignment="Left" Click="Button_Click"/>
</StackPanel>
</Grid>
</Window>
请更改名称pspace WpfAppTest 同时包括上面的示例。 我也建议不要使用后面的代码 并在模型视图类中以相同方式使用MyMVVMEmployeeList 遵循更多的mvvm模式。