Unexpected value changes occur when binding an Enum to GridComboBoxColumn
When binding an Enum to a GridComboBoxColumn, the first item in the list is automatically selected when the ComboBox is opened.
This behavior does not occur when binding other data types, nor when using SfComboBox.
I also tried setting ValueBinding and SelectedValuePath, but the behavior did not change.
In my use case, there is no issue if the user selects a value after opening the ComboBox.
However, if the ComboBox is opened and then loses focus without any selection being made, it closes with the first item automatically selected.
Could you please let me know the reason for this behavior, and if there is any recommended workaround?
<?xml version="1.0" encoding="utf-8" ?>
<Window x:Class="Test.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sfDataGrid="using:Syncfusion.UI.Xaml.DataGrid"
xmlns:sfEditors="using:Syncfusion.UI.Xaml.Editors">
<StackPanel Orientation="Vertical">
<sfEditors:SfComboBox ItemsSource="{x:Bind StrOptions}"
SelectedItem="{x:Bind SelectedStr}"/>
<sfEditors:SfComboBox ItemsSource="{x:Bind EnumOptions}"
SelectedItem="{x:Bind SelectedEnum}"/>
<sfDataGrid:SfDataGrid ItemsSource="{x:Bind ItemsSource}"
AllowEditing="True">
<sfDataGrid:SfDataGrid.Columns>
<sfDataGrid:GridComboBoxColumn MappingName="SelectedEnum"
ItemsSource="{x:Bind EnumOptions}"/>
<sfDataGrid:GridComboBoxColumn MappingName="SelectedStr"
ItemsSource="{x:Bind StrOptions}"/>
</sfDataGrid:SfDataGrid.Columns>
</sfDataGrid:SfDataGrid>
</StackPanel>
</Window>
namespace Test
{
public sealed partial class MainWindow : Window
{
IEnumerable<string> StrOptions = ["StrA", "StrB", "StrC"];
IEnumerable<EnumOption> EnumOptions = Enum.GetValues<EnumOption>();
string SelectedStr { get; set; } = "StrB";
EnumOption SelectedEnum { get; set; } = EnumOption.EnumB;
IEnumerable<Item> ItemsSource = [
new(),
new(),
new()
];
public MainWindow()
{
InitializeComponent();
}
class Item
{
public string SelectedStr
{
get;
set;
} = "StrB";
public EnumOption SelectedEnum
{
get;
set;
} = EnumOption.EnumB;
}
enum EnumOption
{
EnumA,
EnumB,
EnumC,
}
}
}
Hi Ryota_Kuji,
We have reviewed your query and were able to replicate the reported scenario. In DataGrid, SfComboBox control is used for GridComboBoxColumn internally. On further validation, we have found that SfComboBox's SelectedValue property is not updated properly, when set default value (in typeof Enum) on loading. Currently we are validating the issue in SfComboBox control, so we will provide the further update on February 03, 2026.
Regards,
Senthilkumar Ranganathan.
Thank you very much for your prompt response.
I appreciate your support.
As mentioned earlier, the SfComboBox control does not correctly update its SelectedValue property when it is bound to an enum value. We also identified that the same issue occurs with the Microsoft ComboBox. For this , we have created a GitHub query.
GitHub Link: In ComboBox, binding Enum value to SelectedValue is not working
In the DataGrid, we can provide workaround for this issue by customizing the GridCellComboBoxRenderer and overriding the OnInitializeEditElement method. In this method, you can access the SfComboBox instance as the UIElement. When the UIElement.ItemsSource is an enum type, you can subscribe to the SelectionChanging event of the SfComboBox. Within this event, you can manually assign comboBox.SelectedValue to the comboBox.SelectedItem.
Code snippet for customizing the GridCellComboBoxRenderer:
this.dataGrid.CellRenderers.Remove("ComboBox"); this.dataGrid.CellRenderers.Add("ComboBox", new GridCellComboBoxRendererExt()); public class GridCellComboBoxRendererExt : GridCellComboBoxRenderer { public override void OnInitializeEditElement(DataColumnBase dataColumn, SfComboBox uiElement, object dataContext) { base.OnInitializeEditElement(dataColumn, uiElement, dataContext); if (uiElement.ItemsSource is Array arr && arr.GetType().GetElementType()?.IsEnum == true) { uiElement.SelectionChanging += ComboBox_SelectionChanging; } } private void ComboBox_SelectionChanging(object? sender, ComboBoxSelectionChangingEventArgs e) { var comboBox = sender as SfComboBox; if (comboBox != null) { comboBox.SelectedItem = comboBox.SelectedValue; } } } |
This is necessary because, eventhough the SelectedValue initially contains the correct enum value, it becomes null once the SfComboBox is fully loaded. So, it does not assign internally to the underlying base value during the loading process. To overcome this, we are retrieving the ComboBox before it is fully loaded and updating the SelectedValue to the SelectedItem ensures that the Base.SelectedValue is updated correctly.
Gif Illustration:
Please find the sample demo in the attachment.
Attachment: SfDataGridDemo_30a46334.zip
Senthilkumar Ranganathan,
I was able to confirm that the issue can be resolved with minimal changes by applying the improvement you suggested.
I also learned that a bug in WinUI itself may be involved.
Thank you very much for your prompt and helpful response!!
Ryota_Kuji,
Glad that your
requirement is fulfilled!! We are closing this thread now. Please open a new
thread if you need any assistance, we will be happy to help you.
- 5 Replies
- 2 Participants
-
RY Ryota_Kuji
- Jan 29, 2026 04:34 PM UTC
- Feb 4, 2026 05:58 AM UTC