Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null-conditional operators for nested Bindings #17029

Open
Jannik-M91 opened this issue Sep 16, 2024 · 0 comments
Open

Null-conditional operators for nested Bindings #17029

Jannik-M91 opened this issue Sep 16, 2024 · 0 comments

Comments

@Jannik-M91
Copy link

Is your feature request related to a problem? Please describe.

My application uses a "Device" model which represents a physical device that is attached to the PC. The Device model holds all the information that shall be displayed by the UI. For an example a part of the UI shall not be visible while the device is synchronizing:

<StackPanel.IsVisible>
  <MultiBinding Converter="{x:Static BoolConverters.And}">
    <Binding Path="!SelectedDevice.IsSynchronized" />
    <Binding Path="!SelectedDevice.IsSynchronizing" />
  </MultiBinding>
</StackPanel.IsVisible>

The problem is that when no device is connected, "SelectedDevice" will be null causing binding errors even if TargetNullValue or FallbackValue are added.

Also the MultiBinding will not be evaluated, e.g.

<StackPanel.IsVisible>
  <MultiBinding Converter="{x:Static BoolConverters.And}">
    <Binding Path="!SelectedDevice.IsSynchronized" FallbackValue="True" />
    <Binding Path="!SelectedDevice.IsSynchronizing" FallbackValue="True" />
    <Binding Path="!SomeOtherCondition" />
  </MultiBinding>
</StackPanel.IsVisible>

will never be true, regardless of the value of "SomeOtherCondition" as the binding already fails when evaluating "SelectedDevice".

Describe the solution you'd like

An elegant solution has been proposed here: #7857 (reply in thread)

By adding Null-conditional operators to the binding syntax, elements that may be Null could be marked accordingly causing the binding to use the FallbackValue or TargetNullValue instead of failing.

The above example would then look like this:

<StackPanel.IsVisible>
  <MultiBinding Converter="{x:Static BoolConverters.And}">
    <Binding Path="!SelectedDevice?.IsSynchronized" FallbackValue="True" />
    <Binding Path="!SelectedDevice?.IsSynchronizing" FallbackValue="True" />
    <Binding Path="!SomeOtherCondition" />
  </MultiBinding>
</StackPanel.IsVisible>

Describe alternatives you've considered

Currently I work around this by having a "Dummy" device instead of assigning "null" to SelectedDevice when no device is connected.

While this does work in my case, the Null-conditional operators would be a cleaner and more flexible solution.

Additional context

For the original discussion see here: #7857

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants