How do I stop ValueConverters from firing when swapping the content of a ContentControl
Posted
by DanM
on Stack Overflow
See other posts from Stack Overflow
or by DanM
Published on 2010-05-16T20:00:40Z
Indexed on
2010/05/16
20:10 UTC
Read the original article
Hit count: 328
I thought what I was doing was right out of the Josh Smith MVVM handbook, but I seem to be having a lot of problems with value converters firing when no data in the view-model has changed.
So, I have a ContentControl
defined in XAML like this:
<ContentControl Grid.Row="0" Content="{Binding CurrentViewModel}" />
The Window containing this ContentControl
references a resource dictionary that looks something like this:
<ResourceDictionary ...>
<DataTemplate DataType="{x:Type lib_vm:SetupPanelViewModel}">
<lib_v:SetupPanel />
</DataTemplate>
<DataTemplate DataType="{x:Type lib_vm:InstructionsPanelViewModel}">
<lib_v:InstructionsPanel />
</DataTemplate>
</ResourceDictionary>
So, basically, the two data templates specify which view to show with which view-model.
This switches the views as expected whenever the CurrentViewModel
property on my window's view-model changes, but it also seems to cause value converters on the views to fire even when no data has changed. It's a particular problem with IMultiValueConverter
classes, because the values in the value
array get set to DependencyProperty.UnsetValue
, which causes exceptions unless I specifically check for that. But I'm getting other weird side effects too.
This has me wondering if I shouldn't just do everything manually, like this:
- Instantiate each view.
- Set the
DataContext
of each view to the appropriate view-model. - Give the
ContentControl
a name and make it public. - Handle the
PropertyChanged
event for the window. - In the event handler, manually set the
Content
property of theContentControl
to the appropriate view, based theCurrentViewModel
(usingif
statements).
This seems to work, but it also seems very inelegant. I'm hoping there's a better way.
Could you please advise me the best way to handle view switching so that value converters don't fire unnecessarily?
© Stack Overflow or respective owner