0

This isn't so much a problem, but rather an annoyance... I've put together a conditional class to show different views dependent on which radio button is selected. It works as expected, but I can't get rid of the blue squiggle under my 'SwitchCase' property.

Case class:

public class Case : ContentControl
{
    public static readonly DependencyProperty SwitchCaseProperty = DependencyProperty.Register("SwitchCase",
      typeof(object), typeof(Case), new PropertyMetadata() { PropertyChangedCallback = OnPropChanged });

    public object? SwitchCase
    {
        get => GetValue(SwitchCaseProperty);
        set => SetValue(SwitchCaseProperty, value);
    }

    public object? SwitchValue { get; set; }
    public object? SwitchContent { get; set; }

    private static void OnPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is Case _case)
        {
            _case.Content = _case.SwitchCase.ToString() == _case.SwitchValue.ToString() ? _case.SwitchContent : null;
        }
    }
}

Implementation:

<Grid Grid.Row="1"
      TextElement.Foreground="#c2c2c2">
    <conditional:Case SwitchCase="{Binding ElementName=HomeNavButton, Path=IsChecked}"
                      SwitchValue="True"
                      SwitchContent="{StaticResource HomeView}" />
    <conditional:Case SwitchCase="{Binding ElementName=AccountNavButton, Path=IsChecked}"
                      SwitchValue="True"
                      SwitchContent="{StaticResource AccountView}" />
    <conditional:Case SwitchCase="{Binding ElementName=UserNavButton, Path=IsChecked}"
                      SwitchValue="True"
                      SwitchContent="{StaticResource LoginView}" />
</Grid>

It runs fine, but I can't see why the null exception applies when all 3 'navbuttons' exist.

Any ideas?

Edit

The following error appears on each of the SwitchCase statements in xaml editor:

Case.SwitchCase
Object reference not set to an instance of an object

2
  • Please edit your question to include a more detailed description of the problem you have. Hover over the "blue squiggle" and post the full complete error/warning/info message you get. You can also check the error list to see any error/warning/info you get. Commented Jul 19 at 19:28
  • Currently working on C# WPF .Net Core application. The reason I'm using object is so that this can be more generic. The binding could be anything, and the value could be anything. Commented Jul 19 at 19:40

1 Answer 1

1

It turns out that I missed the null checking in the Case class.

private static void OnPropChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    if (d is Case _case && _case.SwitchCase != null && _case.SwitchValue != null)
    {
        _case.Content = _case.SwitchCase.ToString() == _case.SwitchValue.ToString() ? _case.SwitchContent : null;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.