<DataGrID x:name="DataGrIDEmployees" DataContext="{Binding relativeSource={relativeSource AncestorType=Window}}" ItemsSource="{Binding GrIDVIEw}" autoGenerateColumns="True" Loaded="dataGrID1_Loaded" margin="0,2,-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类/ 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" 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>解决方法
myWindow w = new myWindow(); w.DataContext = myviewmodel; w.Show();DataContext="{Binding relativeSource={relativeSource AncestorType=Window}}"
要么
<Window x:name="MyWindow" /> //DataGrID {Binding DataContext,Elementname=MyWindow} ItemsSource="{Binding Myviewmodel.MyList}"总结
以上是内存溢出为你收集整理的WPF C#数据绑定到DataGridTextColumn全部内容,希望文章能够帮你解决WPF C#数据绑定到DataGridTextColumn所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)