I have a question regarding WPF binding using master-detail layout.
The master view contains a DevExpress GridControl with a TreeListView. The detail view content is updated when the TreeListView selected item is changed (trough the binding). The detail view contains just a ContentControl, with it's Content property bound to the current selected item of the TreeListView. In order to display the detail view in a way that correspond to the type of the selected item, several DataTemplates are registered to the current application resources. In my case, the DataTemplates are just one user control.
When the selection is changed in the master view TreeListView, the corresponding DataTemplate is correcly diplayed.
One of my detail view contains a DevExpress ListBoxEdit. The SelectedIndex is bound with UpdateSourceTrigger=PropertyChanged.
My problem is that the setter of the bound property is called with the value -1 when another item is selected in the master view. It seems that the -1 default value is received when the control is unloaded or disposed or unbound. This screw up my view model.
Is there a proper way to avoid to be notified in the ViewModel when the control is unloaded?
[update] The ContentControl that contains the detail view is defined like this:
<ContentControl Grid.Column="1" Content="{Binding CurrentApplicationSettings}" Margin="10,0,0,0"/>
In the corresponding view model (the SetValue method calls OnPropertyChanged):
public IApplicationSettingsViewModel CurrentApplicationSettings
{
get { return GetValue<IApplicationSettingsViewModel>(); }
set { SetValue(value); }
}
In the detail view where I have the problem, the ListBoxEdit is defined like this:
<dxe:ListBoxEdit ShowBorder="False" ItemsSource="{Binding AllLoggingLevels, Mode=OneTime}" SelectedIndex="{Binding Path=LoggingLevel, Mode=TwoWay, Converter={StaticResource LogLevelConverter}, UpdateSourceTrigger=PropertyChanged}">
<dxe:ListBoxEdit.StyleSettings>
<dxe:RadioListBoxEditStyleSettings/>
</dxe:ListBoxEdit.StyleSettings>
</dxe:ListBoxEdit>
In the corresponding ViewModel:
public LoggingLevel LoggingLevel
{
get { return GetValue<LoggingLevel>(); }
set { SetValue(value); }
}
The DataTemplates are registered by code:
public void RegisterDataTemplate(DataTemplate dataTemplate)
{
object dataTemplateKey = dataTemplate.DataTemplateKey;
if (dataTemplateKey == null)
{
throw new InvalidOperationException("The data template has an invalid key");
}
System.Windows.Application.Current.Resources.Add(dataTemplateKey, dataTemplate);
}
Thank you very much Philippe