数据网格 +将列映射到结构字段

发布于 2025-01-05 21:02:00 字数 194 浏览 0 评论 0原文

我正在尝试将数据网格(带有 WPF Toolkit 的 WPF C# 4.0)绑定到结构的字段。我基本上有一个包含两列的数据网格。第一个我想成为标签,第二个我想成为结构的成员。最好我想将这一系列行视为单个结构,然后可以在程序的其他地方使用它。

目前,我让程序为每一行添加某个类的新实例,但我更愿意将它们视为一个......有什么办法吗?

谢谢

I'm trying to bind a datagrid (WPF C# 4.0 with WPF Toolkit) to the fields of a structure. I basically have a datagrid with two columns. The first I would like to be labels, the second I would like to be the members of a structure. Perferably I would like to treat the series of rows as a single structure which can then be used elsewhere in my program.

Currently I have the program adding new instances of some class for each row, but I would prefer to treat it all as one... is there a way?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假扮的天使 2025-01-12 21:02:00

ViewModel

/// <summary>
/// DataGridRowViewModel
/// </summary>
public class DataGridRowViewModel:BaseViewModel
{
    /// <summary>
    /// structureField
    /// </summary>
    private string structureField;

    private string lableText;

    /// <summary>
    /// StructureField        
    /// </summary>
    public string StructureField
    {
        get { return structureField; }
        set
        {
            structureField= value;
            OnPropertyChanged("StructureField");
        }
    }

    /// <summary>
    /// StructureField        
    /// </summary>
    public string LableText
    {
        get { return lableText; }
        set
        {
            lableText= value;
            OnPropertyChanged("LableText");
        }
    }

}



/// <summary>
/// DataGridRowViewModel
/// </summary>
public class MainViewModel:BaseViewModel
{
    /// <summary>
    /// structureField
    /// </summary>
    private ObservableCollection<DataGridRowViewModel> rowCollection; 
    //Make Property with INotifyPropertyChanged

    /// <summary>
    ///   Default Constructor
    /// </summary>
    public MainViewModel()
    {
          RowCollection = new ObservableCollection<DataGridRowViewModel>();
          FillCollectionWithStructureFields();
    }

    private void FillCollectionWithStructureFields()
    {
          //Fill Add New Instances of DataGridRowViewModel with required Label  
          // and Structure Filead Values
    }
}

视图

将集合绑定到具有所需列的 DataGrid...

ViewModel

/// <summary>
/// DataGridRowViewModel
/// </summary>
public class DataGridRowViewModel:BaseViewModel
{
    /// <summary>
    /// structureField
    /// </summary>
    private string structureField;

    private string lableText;

    /// <summary>
    /// StructureField        
    /// </summary>
    public string StructureField
    {
        get { return structureField; }
        set
        {
            structureField= value;
            OnPropertyChanged("StructureField");
        }
    }

    /// <summary>
    /// StructureField        
    /// </summary>
    public string LableText
    {
        get { return lableText; }
        set
        {
            lableText= value;
            OnPropertyChanged("LableText");
        }
    }

}



/// <summary>
/// DataGridRowViewModel
/// </summary>
public class MainViewModel:BaseViewModel
{
    /// <summary>
    /// structureField
    /// </summary>
    private ObservableCollection<DataGridRowViewModel> rowCollection; 
    //Make Property with INotifyPropertyChanged

    /// <summary>
    ///   Default Constructor
    /// </summary>
    public MainViewModel()
    {
          RowCollection = new ObservableCollection<DataGridRowViewModel>();
          FillCollectionWithStructureFields();
    }

    private void FillCollectionWithStructureFields()
    {
          //Fill Add New Instances of DataGridRowViewModel with required Label  
          // and Structure Filead Values
    }
}

View

Bind the collection to DataGrid with required Columns...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文