WPF DataGrid 在 Datatable 和 AutoGenerated Colummns= "True"



我的枚举、数据表和数据网格有问题。我有以下代码:

StateEnum.cs:

public enum StateEnum
    {
        Pass,
        Fail,
        Mandatory,
        Retest
    }

主窗口.xaml.cs:

private void Button_Click(object sender, RoutedEventArgs e)
{
    ViewModel.MainViewModel vm = VmSingleton.GetVmInstance();
    vm.ModuleRecords = new System.Data.DataTable();
    DataTable dt = new DataTable("TableName");
    dt.Columns.Add("Id",typeof(int));
    dt.Columns.Add("Operation1", typeof(StateEnum));
    dt.Columns.Add("Operation2", typeof(StateEnum));
    dt.Columns.Add("Operation3", typeof(StateEnum));
    dt.Columns.Add("Operation4", typeof(StateEnum));
    vm.ModuleRecords = dt;
    for (int i = 0; i < 5; i++)
    {
        DataRow newDR = dt.NewRow();
        foreach (DataColumn col in dt.Columns)
        {
            int j = 1;
            if (col.ColumnName == "Id")
                newDR[col.ColumnName] = i + j;
            else
                newDR[col.ColumnName] = StateEnum.Pass;
            j++;
        }
        dt.Rows.Add(newDR);
    }
}

主窗口.xaml

…
<DataGrid Grid.Row="0" ItemsSource="{Binding ModuleRecords}" AutoGenerateColumns="True" />
<Button Grid.Row="1" Content="Ok" Click="Button_Click"/>
…

虽然Id显示在单元格中,但单元格中的枚举不会直接显示。当我点击一个单元格时,我可以选择正确的枚举。我该怎么修?

编辑:
我很抱歉描述不清。在我的情况下,我想显示数据表的数据,因为我不知道在设计时需要显示多少数据。这就是为什么要选择一个数据表。在我的例子中,我可以做或多或少的手术,但我不知道有多少。。。

我想我在这里发帖之前已经把我的问题简化得太多了。这个代码示例使它更清晰,我希望:

private void Button_Click(object sender, RoutedEventArgs e)
{
    DataAccess.DataAccessor accessor = new DataAccess.DataAccessor();
    // i dont know how much operations i got at design time
    List<string> operationHeaders = accessor.getOperationHeaders();
    ViewModel.MainViewModel vm = VmSingleton.GetVmInstance();
    vm.ModuleRecords = new System.Data.DataTable();
    DataTable dt = new DataTable("TableName");
    dt.Columns.Add("Id",typeof(int));
    // this is because i have to go through with a for-each 
    // to initialize the header of the table
    foreach (string header in operationHeaders)
    {
        dt.Columns.Add(header, typeof(StateEnum));
    }
    vm.ModuleRecords = dt;
    // fil the datatable with data...
    ...
}

我希望现在情况更清楚了。。。

编辑2:
我的问题是枚举值的绑定无法正常工作。我在Viewmodel中设置了值,但datagridview中的枚举属性只显示一个空白单元格。我希望看到枚举属性的值,例如"通过"或"失败"。。。当我点击一个单元格时,我会看到枚举的可能值,但它不是直接设置的。

您不需要使用DataTable,而是使用POCO,在迁移到MVVM时,您的生活会轻松很多。下面的要干净得多
我已经复制了完整的Windows代码,这样你就可以运行演示而不会遇到麻烦,但你必须注意xaml导入的命名空间和Window。参考资料部分:

namespace WpfApplication1
{
    public enum StateEnum
    {
        Pass,
        Fail,
        Mandatory,
        Retest
    }
    public class ModuleRecord
    {
        public int Id { get; set; }
        public StateEnum Operation1 { get; set; }
        public StateEnum Operation2 { get; set; }
        public StateEnum Operation3 { get; set; }
        public StateEnum Operation4 { get; set; }
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            ModuleRecords = new ObservableCollection<ModuleRecord>();
        }
        public ObservableCollection<ModuleRecord> ModuleRecords { get; set; }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                int j = 1;
                var mRecord = new ModuleRecord();
                mRecord.Id = j;
                mRecord.Operation1 = StateEnum.Pass;
                mRecord.Operation2 = StateEnum.Pass;
                mRecord.Operation3 = StateEnum.Pass;
                mRecord.Operation4 = StateEnum.Pass;
                ModuleRecords.Add(mRecord);
            }
            DataContext = this;
        }
    }
}

他们是xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        xmlns:local="clr-namespace:WpfApplication1" 
        xmlns:System="clr-namespace:System;assembly=mscorlib">
    <Window.Resources>
        <ObjectDataProvider x:Key="EnumDataProvider" 
                        MethodName="GetValues" 
                        ObjectType="{x:Type System:Enum}">
            <ObjectDataProvider.MethodParameters>
                <x:Type TypeName="local:StateEnum"/>
            </ObjectDataProvider.MethodParameters>
        </ObjectDataProvider>
    </Window.Resources>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <DataGrid Grid.Row="0" ItemsSource="{Binding ModuleRecords}" AutoGenerateColumns="False">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Id}" Header="Id" />
                <DataGridComboBoxColumn Header="Operation1" ItemsSource="{Binding Source={StaticResource EnumDataProvider}}" SelectedValueBinding="{Binding Operation1}"  />
                <DataGridComboBoxColumn Header="Operation2" ItemsSource="{Binding Source={StaticResource EnumDataProvider}}" SelectedValueBinding="{Binding Operation2}"  />
            </DataGrid.Columns>            
        </DataGrid>
        <Button Grid.Row="1" Content="Ok" Click="Button_Click"/>
    </Grid>
</Window>

相关内容

  • 没有找到相关文章

最新更新