2

As the title suggests; I have a combobox using a composite collection to bind to an observable collection using the MVVM pattern.

If I load my model with existing data then the combobox shows the values so I know the binding works. I can add items to the observable collection and they are shown in a data grid so I know the notify property changed events on the observable collection are working. I suspect it is not working because the composite collection is using a "Static Resource" as its source but if I change it to Dynamic Resource then I get the error:

A 'DynamicResourceExtension' cannot be set on the 'Source' property of type 'Binding'. A 'DynamicResourceExtension' can only be set on a DependencyProperty of a DependencyObject.

I have searched for days to find a solution and while others have faced similar problems the solutions have yet to solve my problem.

Here is my code for the model:

    public class Model : ObservableObject
{
    #region Properties

    private string name;
    public string Name
    {
        get { return this.name; }
        set { this.name = value; }
    }

    private string balance;
    public string Balance
    {
        get { return this.balance; }
        set { this.balance = value; }
    }
    #endregion

My ViewModel:

public class ViewModel : ObservableObject
{
    private ObservableCollection<Model> modelcollection;
    public ObservableCollection<Model> ModelCollection
    {
        get { return modelcollection; }
        set
        {
            modelcollection= value;
            RaisePropertyChangedEvent("ModelCollection");
        }
    }

    private string _name;
    public string Name 
    {
        get { return _name; } 
        set
        {
            _name = value;
            RaisePropertyChangedEvent("Name");
        }
    }
    private string _balance;
    public string Balance 
    {
        get { return _balance; } 
        set
        {
            _balance = value;
            RaisePropertyChangedEvent("Balance");
        }
    }

And finally the XAML of my view for the combobox:

<ComboBox MinWidth="100" SelectedValue="{Binding combovalue, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" SelectedValuePath="Name">
    <ComboBox.Resources>
        <vm:ViewModel x:Key="CollectionKey"/>
    </ComboBox.Resources>

    <ComboBox.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{Binding ModelCollection, Source={StaticResource CollectionKey}, UpdateSourceTrigger=PropertyChanged}"/>
        </CompositeCollection>
    </ComboBox.ItemsSource>

    <ComboBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Path=Name, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"/>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>

Thank you all so much in advance.

EDIT:

So I have moved in a direction; right or wrong is yet to be decided.

I think I have traced the problem to be outside the combobox itself so more context is needed here.

The comobobox is on the second tab of a tab control. Each tab item has its own data context pointing to its own view model. Data is entered on the first tab and I want that data to show up in the combobox on the second tab.

If I put a combobox on the first tab then that combobox updates with changes in the observable collection as entered on the first tab. So the issue, (I think), is that the combobox on the second tab is trying to bind to two different view models at the same time. One for the items source and a different one for the selected value.

There was a suggestion in another thread to use x:Reference for the data context but I can't seem to figure out the correct syntax for that.

If your still reading this then any help is really appreciated.

chris
  • 132
  • 2
  • 11

1 Answers1

0

In the end I never got the combobox to populate from the other view model. I am still unsure if this is possible or not.

The solution that finally worked for me was to just combine everything I needed for binding into a single view model. It made the view model a bit heavy but cut down a lot on the XAML so I guess that's a win.

chris
  • 132
  • 2
  • 11