I have an ObservableCollection of Base Objects bound to a datagrid. I am trying to populate a DataGridComboBoxColumn with Enum values from a derived class instantiated in the observable collection.
- How do I bind an enum from a derived class to a comboboxcolumn?
- How do ensure that the combobox values are bound to the unique enum in the derived instance as I have multiple derived classes that can be instantiated?
Things I've tried
- Create a public list in the base class (CmdsList) and populate it with the enum values from the derived class and bind it that way. but that hasn't worked
ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.Base.CmdsList}"
- Create a data object within Window.Resources and bind as a static resource, but I am unsure of how to make this work with derived classes.
MainWindow
public partial class MainWindow : Window
{
public ObservableCollection<Base> Collection{ get; set; }
public MainWindow()
{
InitializeComponent();
Collection = new ObservableCollection<Base>();
this.DataContext = this;
}
}
Base Class
public abstract class Base: INotifyPropertyChanged
{
public abstract event EventHandler<DataEventArgs> dataUpdate;
public string? addr;
public string? name {get; set;}
public string? port {get; set;}
}
Derived Class 1
public class Derived : Base
{
public override event EventHandler<DataEventArgs>? dataUpdate;
public enum CMDS
{
CMD1 = 0x024F032E,
CMD2 = 0x0253032A,
CMD3 = 0x0252032B,
CMD4 = 0x0250032D,
CMD5 = 0x0270030D,
CMD6 = 0x02570326
}
}
Derived Class 2
public class Derived2 : Base
{
public override event EventHandler<DataEventArgs>? dataUpdate;
public enum CMDS
{
CMD1 = 0x024F332E,
CMD2 = 0x0224F32A,
CMD3 = 0x0252032B,
}
}
XAML
<DataGrid ItemsSource="{Binding Collection}" AutoGenerateColumns="False" CanUserAddRows="False">
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Binding="{Binding name}"></DataGridTextColumn>
<DataGridComboBoxColumn Header="Protocol"></DataGridComboBoxColumn>
<DataGridTextColumn Header="IP Address"></DataGridTextColumn>
<DataGridTextColumn Header="Port" Binding="{Binding port}"></DataGridTextColumn>
<DataGridComboBoxColumn></DataGridComboBoxColumn>
</DataGrid>