views:

164

answers:

1

I have a datagrid whose itemsSource is bound to a multiconverter which uses a converter.

<toolkit:DataGrid AutoGenerateColumns="False">
        <toolkit:DataGrid.ItemsSource>
            <MultiBinding Converter="{StaticResource ProfileConverter}">
                <Binding ElementName="ComboBoxProfiles" Path="SelectedValue" />
                <Binding ElementName="DatePickerTargetDate" Path="SelectedDate" />                   
            </MultiBinding>
        </toolkit:DataGrid.ItemsSource>

This is good because the itemsSource of the grid is updated whenever the combobox or datepicker changes value.

The problem I now have is that in my ViewModel I want to be able to access the ItemSource of my datagrid and either remove items for the list or add new ones.

How do I get access to the itemssource when I have it set up like this?

Many thanks.

+3  A: 

How about having three properties in the ViewModel:

public DateTime? SelectedDate
{
    get{return _selectedDate;}
    set
    { 
         _selectedDate = value;
         UpdateItemsSource();
         OnPropertyChanged("SelectedDate");
    }
}
public object SelectedComboBoxValue
{
    get{return _selectedComboBoxValue;}
    set
    { 
         _selectedComboBoxValue= value;
         UpdateItemsSource();
         OnPropertyChanged("SelectedComboBoxValue");
    }
 }
 private void UpdateItemsSource()
 { 
    _itemsSource = //Some fancy expression based on the two fields.
    OnPropertyChanged("ItemsSource");
 }
 public IEnumerable ItemsSource
 {
     get{return _itemsSource;}
 }

Then bind the datepicker, combobox and datagrid to the respective values.

Hope this helps.

Goblin
Thanks very much. That's the sort of thing I needed. Works nicely.
Steve Scott