16

我很难在此数据网格视图中显示任何数据。我在其他一些 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;
    }
}

我也尝试过XAMLas:

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

2 回答 2

0
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}"
于 2013-09-29T17:16:45.010 回答
0

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

于 2020-08-14T14:05:00.793 回答