Home > Mobile >  Specifying DataContext can cause Xaml to not see it not null when it is?
Specifying DataContext can cause Xaml to not see it not null when it is?

Time:02-17

I have encountered a strange problem in my Wpf application on the Xaml side and I don't have an explanation for it.

Scenario: I have a datagrid binded to some complex classes, some rows have a "details" class with some extra data (VSmtp in the code), while others don't. My plan was to have a small form appear if the user selected a row with that extra data.

To do so I made a custom converter that enabled visibility for a control if the binded object is not null:

    public class NotNullValueToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value != null ? Visibility.Visible : Visibility.Collapsed;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

I binded the visibility of the form to the selected item of the datagrid (SelectedItem in the code) with this converter, tested it and it worked exactly as expected, the form appeared only if the user selected a row and that row had the extra class set

<local:ucSmtpForm 
    Visibility="{Binding SelectedItem.VSmtp, Converter={StaticResource NotNullValueToVisibilityConverter}}"
    >
</local:ucSmtpForm>

Happy with the results, i proceeded to bind the data to the form, however when i did that and tested again, I discovered that the form now appeared on every row, even those without the VSmtp class

<local:ucSmtpForm 
    Visibility="{Binding SelectedItem.VSmtp, Converter={StaticResource NotNullValueToVisibilityConverter}}"
    DataContext="{Binding SelectedItem.VSmtp}"
    >
</local:ucSmtpForm>

I have no idea for the reason the form now is visible on rows without the VSmtp class while before it worked fine, debugging the code confirms that the property is null where it should be.

Does anyone have an explanation for this?

I have already solved the issue by simply putting the form in a grid and moved the Visibility binding to it, now works as intended, but I wanted to know why it didn't work if the DataContext and Visibility were both binded in the UserControl, to be better informed for the future.

Thanks in andvance

CodePudding user response:

Before you set DataContext explicitly the DataContext for local:ucSmtpForm was SomeViewModel, which had SelectedItem property.
As you set DataContext for local:ucSmtpForm to new one, namely to SelectedItem.VSmtp the binding for Visibility was broken, because VSmtp has not a SelectedItem property. This should work:

<local:ucSmtpForm 
    Visibility="{Binding '.', Converter={StaticResource NotNullValueToVisibilityConverter}}"
    DataContext="{Binding SelectedItem.VSmtp}"
    >
</local:ucSmtpForm>
  • Related