Search code examples
c#wpfbindingivalueconverter

Only some Converters fire?


I am working on a project in WPF and I have a very strange case concerning my converters on a certain element.

In the following snippet:

<myCtl:Pager IsTabStop="False" Style="{StaticResource MainPager}"
                DataContext="{Binding CurrentView, Converter={StaticResource SectionToPagerDriver}}"
                Visibility="{Binding CurrentView, Converter={StaticResource SectionToVisibility}}"/>

The converter for 'DataContext' will fire, but the converter for 'Visibility' will not. This seems odd to me considering that they are both bound to 'CurrentView' which indeed changes. I have even tried setting the binding mode explicitly to 'TwoWay' but this does nothing to resolve the issue.

Does anyone have a clue why one binding would fire, and the other would not ?


Solution

  • When you set the DataContext on your Control, all other bindings will use the new object as their source.

    If you check your output window, you'll see a binding error saying that there is no CurrentView property on whatever object is returned by that property.

    Instead, you should just do:

    <myCtl:Pager IsTabStop="False" Style="{StaticResource MainPager}"
                 DataContext="{Binding CurrentView, Converter={StaticResource SectionToPagerDriver}}"
                 Visibility="{Binding Converter={StaticResource SectionToVisibility}}"/>