In WinUI 3, CommunityToolkit.WinUi, DataGridComboBoxColumn there is a Binding problem

232 Views Asked by At

In my view model class, there is an [ObservableProperty] called EmployeesList of type ObservableCollection<Employee>.

Similarly there is another [ObservableProperty] called LeavesList of type ObservableCollection<Leave>.

Both are populated asynchronously using a service.

On my page there is a DataGrid that is bound to the LeavesList (ItemsSource="{x:Bind viewModel.LeavesList}).

One of its columns is a DataGridComboBoxColumn represents the name of the employee.

As usual the ItemsSource of the DataGridComboBoxColumn is bound to the employees list.

The value of the DataGridComboBoxColumn is bound to a property of Leave namely EmployeeId.

The exact problem is originated form the fact that Leave.EmployeeId is actually an FK that points to Employee.Id but the documentation states that these MUST be of the same name for the binding to work.

Docs

I am not in the business to change model classes just to please XAML front end.

So what I did is a dirty and disgusting solution which is to map ObservableCollection<Employee> to a new ObservableCollection<TempEmployeeClassThatHasEmployeeIdInsteadOfId> just to rename the primary key name to EmployeeId instead of Id to match the FK in the Leave model class.

Is there a cleaner way to do it using XALM ? or C# in the view model or somewhere else that is reusable, without temporary objects and workarounds?

<controls:DataGridComboBoxColumn
    Binding="{Binding EmployeeId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    DisplayMemberPath="Name"
    Header="Employee Name"
    ItemsSource="{x:Bind viewModel.TempEmployeeListWithIdReWrittenToEmployeeIdJustToMatchLeave_EmployeeId, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
    Tag="Employee Name" />

I need to circumvent the requirement:

The ItemsSource elements of the column must have at least one property with the same name as the elements of the ItemsSource belonging to the DataGrid itself.

(from Docs)

0

There are 0 best solutions below