Hello,
I am struggeling with binding to GridComboBoxColumn. It changes the source list of values instead of the collection in grid.
Model (MeetingMinutesItemType):
public class MeetingMinutesItemType: ObservableObject
{
private string name = "";
public string Name { get => name; set => Set(nameof(Name), ref name, value); }
private string? description;
public string? Description { get => description; set => Set(nameof(Description), ref description, value); }
}
Model (MeetingMinutesItem):
public class MeetingMinutesItem: ObservableObject
{
private string? note;
public string? Note { get => note; set => Set(nameof(Note), ref note, value); }
private MeetingMinutesItemType? type;
public MeetingMinutesItemType? Type { get => type; set => Set(nameof(Type), ref type, value); }
}
ViewModel (SidekickMainViewModel)
public class SidekickMainViewModel
{
public ObservableCollection<MeetingMinutes> MeetingMinutes { get; } = new();
public ObservableCollection<MeetingMinutesItemType> MeetingMinutesItemTypes { get; } = new();
}
Model
<syncfusion:SfDataGrid AutoGenerateColumns="False"
ItemsSource="{Binding MeetingMinutes[0].Items}"
AllowResizingColumns="True"
AllowDraggingColumns="True"
AllowDraggingRows="True"
AllowEditing="True"
AllowDeleting="True"
AllowGrouping="False"
EditTrigger="OnTap">
<syncfusion:SfDataGrid.Columns>
<syncfusion:GridComboBoxColumn HeaderText="Typ"
UseBindingValue="True"
MappingName="Type.Name"
DisplayMemberPath="Name"
SelectedValuePath="Name"
ValueBinding="{Binding Path=Type}"
DisplayBinding="{Binding Path=Type.Name}"
ItemsSource="{Binding MeetingMinutesItemTypes, Source={StaticResource MainViewModel}}"/>
<syncfusion:GridTextColumn MappingName="Note"
TextWrapping="Wrap"
HeaderText="Poznámka"/>
</syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>
Whole code is attached.
This code shows all necessary. The problem is when I want to change the type od the Items. The value is not changed in MeetingMinutes[0].Items, but in MeetingMinutesItemTypes, which is definitely something I do not want.
Where am I doing a mistake?
Cheers.
Hi Slávek,
After analyzing the scenario, you reported, we have identified that the issue is related to the usage of ValueBinding with a Class type. It seems that you are attempting to commit the value of a Type to a String property, which is causing the issue you reported. To address this issue, we recommend using ValueBinding with the same type (String) as the property.
We have updated the sample code to include the necessary changes, and we have attached the modified version for your reference. Please have a look at this.
Regards,
Dhanasekar M.
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Hi
Dhanasekar,
I am not sure if we understand each other well. I don't want to change valeus in the list of values (MeetingMinutesItemTypes), but I need to change the type of selected item in MeetingMinutes.
The solution you recommended is changing the values in the LOV:
As you can see, there is now Information on two lines, which is wrong.
How should I change the XAML?
Thanks.
Slávek,
We would like to let you know that the cell value of
GridComboBoxColumn will be updated only while changing the underlying property
(MappingName) that is bound to the column. The reported problem occurs because
the MappingName bound property is a class (ex: person) type and not a property.
Also, the Binding/Members are used with the class reference. Here we have
modified the sample as shown below,
Model:
public class MeetingMinutesItem: ObservableObject { private string? note; public string? Note { get => note; set => Set(nameof(Note), ref note, value); }
private DateTime createdAt = DateTime.Now; public DateTime CreatedAt { get => createdAt; set => Set(nameof(CreatedAt), ref createdAt, value); }
private Stakeholder? responsiblePerson; public Stakeholder? ResponsiblePerson { get => responsiblePerson; set => Set(nameof(ResponsiblePerson), ref responsiblePerson, value); }
private string? type; public string Type { get => type; set => Set(nameof(Type), ref type, value); }
} |
ViewModel
public SidekickMainViewModel() { MeetingMinutesItemTypes.Add(new() { Name = "Task", Description = "Úkol, který je třeba udělat", GuiName = "Task (Úkol)" }); MeetingMinutesItemTypes.Add(new() { Name = "Information", Description = "Informace, která byla zdělena", GuiName = "Information (Informace)" }); MeetingMinutesItemTypes.Add(new() { Name = "Decision", Description = "Rozhodnutí, které bylo učiněno", GuiName = "Decision (Rozhodnutí)" }); MeetingMinutesItemTypes.Add(new() { Name = "Reflection", Description = "Úvaha, která byla pronesena bez dalšího rozhodnutí", GuiName = "Reflection (Úvaha)" });
MeetingMinutes.Add(new MeetingMinutes()); MeetingMinutes[0].Items.Add(new() { Note = "Hello", Type = MeetingMinutesItemTypes[0].Name }); MeetingMinutes[0].Items.Add(new() { Note = "Cheers", Type = MeetingMinutesItemTypes[1].Name }); MeetingMinutes[0].Items.Add(new() { Note = "Good night", Type = MeetingMinutesItemTypes[2].Name });
SelectedMeetingMinutesItem = MeetingMinutes[0].Items[0]; } |
XAML:
<syncfusion:GridComboBoxColumn HeaderText="Type" MappingName="Type" SelectedValuePath="Name" DisplayMemberPath="Name" ItemsSource="{Binding MeetingMinutesItemTypes, Source={StaticResource MainViewModel}}"/> |
Here we have attached the modified sample for your reference.
If this post is helpful, please consider Accepting it as the solution so that other members can locate it more quickly.
Hi
Dhanasekar,
cheers for clarification, now I got it. Is there any other way how to bound to a class (maybe another type of your grid)?
Thanks.
Slávek,
We are a little unclear about your requirement “Is there any other way how to bound to a class (maybe another type of your grid)?”. If you need to populate the columns for the complex type properties, then it will be achievable by using the AutoGenerateColumnsForCustomType and AutoGenerateColumnsModeForCustomType. For more information related to AutoGenerateColumnsForCustomType and AutoGenerateColumnsModeForCustomType, please refer to the below user guide documentation link,
UG Link: https://help.syncfusion.com/wpf/datagrid/columns#auto-generating-columns-for-complex-type
If we misunderstood your requirement, please provide more information regarding the requirement. This would help us to proceed further.