bogdanbrudiu
bogdanbrudiu

Reputation: 564

silverlight binding problem nested controls

I have a binding problem in silverlight and I cannot make it work...

I have a control which has a model behind.

The control code behind looks like this

.....
 public ClientListingVM()//constructor
        {
            ClientListingViewModel model = new ClientListingViewModel();//this is my model
            DataContext = model;
            InitializeComponent();
        }
.....

the model code behind looks like this

....
  public ObservableCollection<FilterItemDefinition> FilterItemDefinitions
        {
            get { return _filterItemDefinitions; }
            set
            {
                if (_filterItemDefinitions != value)
                {
                    _filterItemDefinitions = value;
                }
                OnPropertyChanged("FilterItems");
            }
        }
....

In the control xaml I have this

....
  <my:Filters Height="150" FilterItems="{Binding Path=FilterItemDefinitions}" Columns="{Binding ElementName=ClientGrid, Path=Columns}"/>

....

where Filters is a user control... Until here everything works ok...

The code behind for filters looks like this

....
 public partial class Filters : UserControl
    {
        public Filters()
        {
            InitializeComponent();

        }
        private DependencyProperty FilterItemsProperty = DependencyProperty.Register("FilterItems", typeof(ObservableCollection<FilterItemDefinition>), typeof(Filters), new PropertyMetadata(null, new PropertyChangedCallback(OnChangef)));
        public ObservableCollection<FilterItemDefinition> FilterItems
        {
            get
            {
                return (ObservableCollection<FilterItemDefinition>)GetValue(FilterItemsProperty);
            }
            set
            {
                SetValue(FilterItemsProperty, value);
            }
        }
        private DependencyProperty ColumnsProperty = DependencyProperty.Register("Columns", typeof(ObservableCollection<DataGridColumn>), typeof(Filters), new PropertyMetadata(null, new PropertyChangedCallback(OnChangec)));
        public ObservableCollection<DataGridColumn> Columns
        {
            get
            {
                return (ObservableCollection<DataGridColumn>)GetValue(ColumnsProperty);
            }
            set
            {
                SetValue(ColumnsProperty, value);
            }
        }
        public static void OnChangef(DependencyObject d, DependencyPropertyChangedEventArgs e)
        { 
        }
        public static void OnChangec(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }
    }
....

and the xaml looks like this

....
 <ListBox x:Name="myListBox" ItemsSource="{Binding Path=FilterItems, Mode=TwoWay}" >
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <my:FilterItem Columns="{Binding Source={StaticResource DataContextProxy},Path=DataSource.Columns}"/>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
....

So here is the problem:

the ListBox ItemSource is bound to FilterItems... but the Filters DataContext is ClientListingViewModel... and that model does not have FilterItems property.

If in the Filters constructor I set a FiltersViewModel or I say DataContext=this the Filters DataContext will have the property FilterItems and the binding willwork ok.. But the property FilterItemDefinitions from ClientListingViewModel will not get to my control...

I would like that the DataContext for FiltersControl to be his DataContext or the code behind.. but in the same time the binding of his properties with the parent control model to work.. I am sure this is possible and I am doing it in the wrong way.. can someone please help me ... 10x

Upvotes: 2

Views: 1103

Answers (1)

AnthonyWJones
AnthonyWJones

Reputation: 189495

Leave your existing DataContext alone, you don't want to be doing things like DataContext = this. Instead bind ItemsSource like this:-

ItemsSource="{Binding Path=Parent.FilterItems, ElementName=LayoutRoot}"

This assumes you have default root element named as LayoutRoot. Hence the Parent of this root will be your Filters user control which has a FilterItems property. BTW Two way binding on this property doesn't make a lot of sense.

Upvotes: 2

Related Questions