WPF C# 数据绑定到 DataGridTextColumn
本文关键字:DataGridTextColumn 数据绑定 WPF | 更新日期: 2023-09-27 18:33:07
我很难显示此数据网格视图中的任何数据。我已经遵循了其他一些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
作为:
<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>
我建议在我的窗口中使用ICollectionView界面或在我的模型视图类中更好
下面是一个示例 MainWindow.xaml.cs 类,它演示了如何通过代码隐藏将数据添加到此类接口 类:
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;
}
}
}
然后,MainWindow.xaml 中的绑定应如下所示:
<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>
为了使它在这里完全可运行您的数据类 MVVMEmployee.cs再次不引用基类:
namespace WpfAppTest
{
public class MVVMEmployee
{
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;
}
}
}
请更改名称空间 WpfAppTest同时包括上面的例子。我还建议不要使用后面的代码并在模型视图类中以相同的方式使用 MyMVVMEmployeeList以遵循更多 MVVM 模式。
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}"
你试图如何实现它根本不是要走的路。首先,您需要分离关注点并避免混合视图和视图模型。在 ViewModel 中,创建对象的可观察集合(在您的例子中为 MVVMEmployee(,该集合应作为单独的模型实现,其中 NotifyPropertyChanged 事件已实现并附加到您的属性。然后只需在 Window.cs 构造函数中创建 ViewModel 的一个实例(我们称之为 vm(并设置它。数据上下文 = 虚拟机;然后将 DataGrid 的 ItemsSource 绑定到您的集合中,就像 ItemsSource = {Binding MyEmployeesCollection} 一样(不需要 RelativeSource(。然后绑定每个文本列,例如绑定 = {绑定全名}。并且不要忘记在您的模型和视图模型上实现 INotifyPropertyChanged。这是以干净的 MVVM 方式实现任务的方法之一。还有其他清洁的方法。即,您可以将 DataContext 直接绑定到 Window.xaml 中的 ViewModel 实例。
尝试将属性添加到视图模型 在 Xaml "GridView" 中使用相同的属性绑定,则应Iobservablecollection
此属性 Type 以引发RaisePropertyChanged()
方法例:
private ObservableCollection<Employee> _GridView = new
ObservableCollection<Employee>();
public ObservableCollection<Employee> Publishers
{
get { return _GridView; }
set { _GridView = value; RaisePropertyChanged(); }
}
请注意,您必须在 ViewModel 中实现 INotifyPropertyChanged 接口,并将视图的 DataContext 设置为您的 ViewModel,如下所示:
ViewModel WM = new ViewModel ();
this.DataContext = WM ;