Csharp/C#教程:DataGridTemplateColumn中的ComboBox不显示SelectedItem分享


DataGridTemplateColumn中的ComboBox不显示SelectedItem

我想制作包含ComboBox的自定义DataGrid列。 combobox的ItemSource绑定到枚举,combobox的选择项与集合元素中选择的枚举值绑定。

这是代码

                         

代码隐藏

 public partial class MainWindow : Window { AnimalObservableCollection animals; public MainWindow() { InitializeComponent(); animals = (AnimalObservableCollection)this.FindResource("animals"); animals.Add(new Animal(AnimalType.horse,"Rex")); } } public enum AnimalType { dog, cat, horse }; class AnimalObservableCollection : ObservableCollection { public AnimalObservableCollection() : base() { } } class Animal { private AnimalType animalType; private string name; public Animal() { } public Animal(AnimalType animalType_, string name_) { animalType = animalType_; name = name_; } public AnimalType AnimalType { get { return animalType; } set { animalType = value; } } public string Name { get { return name; } set { name = value; } } } 

问题是,当combobox失去焦点时,选择项不会在DataGrid的单元格中显示,并且此单元格保持空白。 如何在AnimalType列中创建单元格来显示combobox的选择?

当我使用DataGridComboBoxColumn时,它工作得很好,但是我想在将来使用DataGridTemplateColumn添加更多function。

在您的代码中,我只看到CellEditingTemplate,这意味着只有当单元格处于编辑模式时才为此单元格定义模板,您还需要定义CellTemplate,该单元格在单元格处于“视图”模式时使用。 像这样:

上述就是C#学习教程:DataGridTemplateColumn中的ComboBox不显示SelectedItem分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

             

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1015147.html

(0)
上一篇 2021年12月30日
下一篇 2021年12月30日

精彩推荐