My grid has a comboBox column. But my binding isn't quite working the way I want. Here is my code:
<
telerik:GridViewComboBoxColumn
Header
=
"Action"
UniqueName
=
"Action"
DataMemberBinding
=
"{Binding ActionID, Mode=TwoWay, ValidatesOnDataErrors=True}"
SelectedValueMemberPath
=
"ActionID"
DisplayMemberPath
=
"Name"
ItemsSource
=
"{Binding DataContext.Actions, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=UserControl,AncestorLevel=2}}"
IsComboBoxEditable
=
"True"
>
<
telerik:GridViewComboBoxColumn.CellTemplate
>
<
DataTemplate
>
<
TextBlock
Text
=
"{Binding Action.Name}"
/>
</
DataTemplate
>
</
telerik:GridViewComboBoxColumn.CellTemplate
>
</
telerik:GridViewComboBoxColumn
>
When the row loads, the value for this comboBox displays. But when the user changes it or selects a value for a new row, it doesn't display the value until the row is committed. I know this is because I'm displaying the value of Action.Name, but binding the selected value to ActionID.
Is there any way I can get it to show the Name property before its been committed? I could if I don't use a CellTemplate and just use the comboBox, but then there are some records that have an action that isn't a current available option in the drop down. So if I don't use the CellTemplate, then those will display blank because there isn't a matching item in the drop down. Make sense?
How do I accomodate both?
7 Answers, 1 is accepted
Would you please take a look at this forum thread ? Do you want to achieve something similar ?
Maya
the Telerik team
Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>
When the grid binds to the collection, it's displaying the child property Action.Name in the cell using the CellTemplate defined. The comboBox column is binding to the ActionID value of the data (not the Action navigational property). So once the user selects an item from the combobox, the value is blank, because the celltemplate displays Action.Name and until the ActionID is committed, there is no Action property.
If I remove the CellTemplate Data Template, then it works. It displays the Name value associated with the selected ActionID. But for existing rows, there may be one that has and Action chosen that has now been "de-activated" so it's not longer in the collection that the combobox uses. So that row shows a blank (no matching record in the combobox itemssource).
What I need is to be able to display those actions regardless of whether they are in the ComboBox ItemsSource or not (hence the reason for the CellTemplate binding to Action.Name), but then it doesn't show up when the row is in EditMode (once an Action has been selected and the user moves to next cell in row).
Indeed, generally, you cannot display a value in GridViewComboBoxColumn that is not in its ItemsSource. Will it be possible to share some code-snippets about the implementation of your two business objects - the one for the grid and the one for the column and the definitions of your column ?
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Action (this is the ItemsSource of the ComboBoxColum):
- ActionID
- Name
- Active
- RowVersion
Over time, some actions might become inactive as business rules change. So I'm using a Linq query to populate an ObservableColleciton where Active is true. This is the list of available choices the user can pick from.
_context.Actions.Where(a => a.Active).OrderBy(a => a.Name);
The business model for the row has a scalar property ActionID and a navigational property Action, where the ActionID property is the foreign key association for the Action property. The business model is in Entity Framework.
I want to display the Action regardless of whether it is currently an available choice or not. If they choose to edit that record, then they have to choose a different Action from the list of valid choices.
There's got to be a way to do it. I had tried setting a CellTemplate to show Action.Name so that it displays the existing value. It worked for existing rows. But for new rows the cell is blank while the row is in edit mode, because it doesn't assign the Action property based on the ActionID (foreign key) property until the row is committed. So the user can't see what action they selected until after they are done editing the row.
Would you try to handle SelectionChanged event of RadComboBox and commit the changes:
this.AddHandler(RadComboBox.SelectionChangedEvent, new SelectionChangedEventHandler(OnSelectionChanged));
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.RemovedItems.Count != 0)
{
//this ensures a selection from the drop down is performed.
this.playersGrid.CommitEdit();
}
}
Thus once you select an item from the drop down, the change will be committed.
Will that approach work in your case ?
Greetings,
Maya
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Although MVVM is considered to be the standard XAML UI architectural pattern, following it completely remains a matter of personal choice. Most of the standard non-MVVM techniques have valid MVVM-friendly counterparts. We prefer to provide the most basic solution that focuses on the problem itself, instead of overengineering it without having the full context of the client's project. These solutions are left open for further interpretations and we are always glad to discuss them with our clients.
I am attaching a modified version of the discussed approach that uses the Evend-To-Command pattern to avoid direct event handling in the view's partial class.
Regards,
Ivan Ivanov
Telerik