I'm running into a difficult binding situation. I'm using T开发者_开发百科elerik RadGridView with GridViewComboBoxColumn. I've got two combo boxes a "From" and a "To" bound to separate List<int>
. When the user drops the From combo box open the values presented should reflect an "available" range of integers. The available integers consists of the range of integers in the current rows From and To range minus the ranges in the other rows.
The problem I'm having is that the From combo box is a bound List<int>
and when I create the available integers list it removes the range of integers found in all the other non-selected rows. For example if I have a List<int>
1-48 and the first row has a range of 25-36 and the second row has a range of 37-42. When the user drops the From combo box open on the first row they are presented with a list 1-36 and 43-48. The range 37-42 is not available because it's in use on the second row.
Row 1 From = 25 To = 36 Row 2 From = 37 To = 42
THIS IS THE PROBLEM: Because the bound list doesn't contain the integers 37-42 what happens is the selected item for the second row is no longer available in the list and won't display in the grid. Row two's From combo box should display '37' after Row 1's From combo box closes. I need a way to remember the values of non selected rows and display those values in the From combo box even though they aren't found in the bound list.
FYI: I'm not concerned about the TO combo box here, it's likely any solution for FROM can also be applied to the TO combo box.
I know this is confusing please ask if you have questions. Basically I need a way to retain the non-selected rows selected items when those items are no longer present in the bound list. I only need to display the original selected value. The items in the combo box for the second row are of no importance until it's drop down is opened. Then this situation reverses and I would need to be able to recall the selected item for the first row.
telerik:GridViewComboBoxColumn x:Name="cboFrom2" Header="From" Width="Auto"
IsFilterable="False" IsGroupable="False" IsReorderable="False" IsSortable="False"
DataMemberBinding="{Binding StrandFrom}" EditTriggers="CellClick" >
<telerik:GridViewComboBoxColumn.EditorStyle>
<Style TargetType="telerik:RadComboBox">
<Setter Property="OpenDropDownOnFocus" Value="True"/>
</Style>
</telerik:GridViewComboBoxColumn.EditorStyle>
</telerik:GridViewComboBoxColumn>
If I understand you correctly, what about making use of a multi converter that combines the list of available options with the selected option?
In your ViewModel
, keep an ObservableCollection
of available options. This is any number that is not being used by your data items. Whenever a data item's To
or From
property changes, alter the list of Available options.
Then bind your ComboBoxes to that list of available options, and use a multi converter to add the currently selected item to the ItemsSource
Here's some rough pseudo-code. I didn't run it through a compiler or anything, but it should give you the general idea.
View Model
public ObservableCollection<int> AvailableNumbers;
public ObservableCollection<MyClass> DataObjects;
foreach(var obj in DataObjects)
{
obj.PropertyChanged += DataObject_PropertyChanged;
}
void DataObject_PropertyChanged(src as object, e as PropertyChangedEventArgs)
{
if (e.PropertyName == "To" || e.PropertyName == "From")
// Adjust AvailableNumbers list
}
XAML
<ComboBox SelectedItem="{Binding From}">
<ComboBox.ItemsSource>
<MultiBinding Converter="{StaticResource CombineListAndObjectMultiConverter}">
<Binding RelativeSource="{RelativeSource AncestorType={x:Type DataGrid}}"
Path="DataContext.AvailableNumbers"/>
<Binding Path="From" />
</MultiBinding>
</ComboBox.ItemsSource>
</ComboBox>
And your CombineListAndObjectMultiConverter
would simply add an object to a list and return the list.
精彩评论