从代码生成的 WPF DataGridComboBoxColumn

发布于 2025-01-12 08:58:58 字数 1318 浏览 2 评论 0原文

我正在从与数据表关联的可编辑数据网格视图中包含的代码生成组合格式的列。

当我将自己置于单元格中时,会出现带有值的组合,但我无法在单元格中显示 SelectedValue。事实上,在初始数据加载中,该单元格不再显示数据列表的值(我收到数据表中的值 0,1 或 2),并且我想不知何故我不知道如何创建必要的绑定。

protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e)
        {
           if ( e.PropertyName == "combocolumn") {
                    var cb = new DataGridComboBoxColumn();
                    cb.Header = "ColumnCombo";
                    cb.Visibility =  Visibility.Visible;
                    cb.IsReadOnly = false;
                    cb.DisplayMemberPath = "Descripcion";
                    cb.SelectedValuePath = "Codigo";
                    cb.ItemsSource = Datos;
                    
                    e.Column = cb;
                    
                  }
            
            
            base.OnAutoGeneratingColumn(e);
        }

// ===> datos ...
 public class DatosCombo
    {
        public int Codigo { get; set; }
        public string Descripcion { get; set; }
        
    }


 private List<DatosCombo> Datos= new List<DatosCombo>()
        {
            new DatosCombo() { Codigo = 0,Descripcion="Dato1"},
            new DatosCombo() { Codigo = 1,Descripcion="Dato2"},
            new DatosCombo() { Codigo = 2,Descripcion="Dato3"}
        };

I am generating a column in combo format from code that is included in an editable datagridview associated with a DataTable.

When I place myself in the cell, the combo with the values ​​appears, but I can't get the SelectedValue to be displayed in the cell. In fact, in the initial data load, that cell no longer shows the value of the data list (I receive the values, 0,1, or 2 in the data table), and I suppose that somehow I have not known how to create the binding necessary.

protected override void OnAutoGeneratingColumn(DataGridAutoGeneratingColumnEventArgs e)
        {
           if ( e.PropertyName == "combocolumn") {
                    var cb = new DataGridComboBoxColumn();
                    cb.Header = "ColumnCombo";
                    cb.Visibility =  Visibility.Visible;
                    cb.IsReadOnly = false;
                    cb.DisplayMemberPath = "Descripcion";
                    cb.SelectedValuePath = "Codigo";
                    cb.ItemsSource = Datos;
                    
                    e.Column = cb;
                    
                  }
            
            
            base.OnAutoGeneratingColumn(e);
        }

// ===> datos ...
 public class DatosCombo
    {
        public int Codigo { get; set; }
        public string Descripcion { get; set; }
        
    }


 private List<DatosCombo> Datos= new List<DatosCombo>()
        {
            new DatosCombo() { Codigo = 0,Descripcion="Dato1"},
            new DatosCombo() { Codigo = 1,Descripcion="Dato2"},
            new DatosCombo() { Codigo = 2,Descripcion="Dato3"}
        };

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

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

发布评论

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

评论(1

夏末染殇 2025-01-19 08:58:58

如果它对更多人有用,我已经通过添加解决了这个问题:

cb.SelectedValueBinding = new System.Windows.Data.Binding("DatosCombo");

In case it is useful for more people, I already solved the problem by adding :

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