WPF C# 数据绑定到 DataGridTextColumn

Posted

技术标签:

【中文标题】WPF C# 数据绑定到 DataGridTextColumn【英文标题】:WPF C# Data-binding to DataGridTextColumn 【发布时间】:2013-10-05 04:23:40 【问题描述】:

我很难在此数据网格视图中显示任何数据。我在其他一些 *** 论坛帖子中遵循了一些建议,但没有任何运气让内容出现。

<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 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>

【问题讨论】:

我认为您正在将 ItemsSource 设置为 Window 上的 GridView 属性,这样的属性是否存在? EmployeeDetails ed = new EmployeeDetails(); ed.DataContext = ed.DomainEmployees; ....在这里,您正在为 ed 的数据上下文分配 ed 上的项目?对吗? EmployeeDetails ed = new EmployeeDetails(); ed.DataContext = ed.DomainEmployees; DomainEmployees 是视图模型的 ObservableCollection,而不是单个项目。 我投票结束这个问题,因为 OP 没有费心发布他们遇到的确切问题的详细信息,这是一个老问题,没有正确的答案,并且 OP 自 2013 年 12 月以来就没有出现过。 【参考方案1】:
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"

【讨论】:

【参考方案2】:

我建议在我的窗口中使用 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 模式。

【讨论】:

以上是关于WPF C# 数据绑定到 DataGridTextColumn的主要内容,如果未能解决你的问题,请参考以下文章

WPF C# DataGrid 绑定到存储过程

DatePicker 数据绑定将默认值设置为 1/1/0001 WPF c#

在 C# 中将 WPF 属性绑定到 ApplicationSettings 的最佳方法?

C# WPF 数据绑定

C# WPF 数据绑定DataContext;Window_Loaded时进行过数据绑定,指定DataContext;触发另一事件?

C#:WPF MVVM 中的按钮绑定