Get notified of DataContext changed in WPF resource

I am having a frustrating issue with WPF binding. Basically, I declare FrameworkElement in my UserControl resources, but that element doesn't seem to be notified when the DataContext of the parent UserControl changes.

Basically, in my UserControl, I have a popup in the ItemTemplate for the ItemsControl. In this popup, I needed to bind something in the parent ViewModel, so I came up with what I thought was smart. Taking the signal from the CollectionViewSource, I figured I would just bind the parent view model to the resource, and then use that resource to navigate to the ViewModel from the DataTemplate, like so:

    <UserControl.Resources>
        <cont:ViewModelSource Source="{Binding}" x:Key="ParentViewModel"/>
    </UserControl.Resources>

      

Therefore, so that later I can use it like:

CommandParameter="{Binding ViewModel.OpenEntity, Source={StaticResource ParentViewModel}}"

      

This all works, except the DataContext in the ViewModelSource doesn't get reset when the DataContext UserControl gets reset. Right now I'm making this work a hack: setting an encoded DataContext resource on the UserControl DataContextChanged event.

I took a look at Reflector to see how the CollectionViewSource does it, but it doesn't seem to do anything special.

Does anyone know why this is happening or how I can fix it?

+2


a source to share


2 answers


I had the same problem and found a solution.

I first tried setting my ViewModel as the DataContext of my root element. wrong.

Then I tried to set my ViewModel as a resource and set the binding source of my root to the resource. wrong.



Finally, I created an IValueConverter to transform the model (after all, this is the DataContext of the control) into a ViewModel. Then I bind the DataContext to the root element with the converter.

<UserControl.Resources>

    <local:PersonToControllerConverter x:Key="PersonToControllerConverter"/>

    <!--<local:PersonController x:Key="controller"
        Value="{Binding}"
        Parent="{Binding Path=DataContext,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}"
        />-->

</UserControl.Resources>



<Border x:Name="root" BorderBrush="Black" BorderThickness="2" >
    <Border.DataContext>
        <MultiBinding Converter="{StaticResource PersonToControllerConverter}">
            <Binding/>
            <Binding Path="DataContext" RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}"/>
        </MultiBinding>
    </Border.DataContext>

    <!--DataContext="{Binding Source={StaticResource controller}}">-->

    <!--<Border.DataContext>
        <local:PersonController
                    Value="{Binding}"
                    Parent="{Binding Path=DataContext,RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type TreeViewItem}}}"
                    />
    </Border.DataContext>-->

      

I think that when the DataContext breaks the binding in the element, when the datacontext changes on the root element, it stops at the broken binding.

+2


a source


Perhaps you need to create a middle object that implements the interface INotifyPropertyChanged

.



+1


a source







All Articles