How to negate bool value inside blazor?
Asked Answered
F

1

10

I'm trying to do this

<MatCheckbox @bind-Value="@(!Checked)" Label="Checkbox"></MatCheckbox>

But it gives me the error

CS0131 The left-hand side of an assignment must be a variable, property or indexer

How can I pass a negated boolean value without creating another property?

I want to bind the value so it's !Checked

Flaring answered 9/3, 2020 at 14:4 Comment(5)
Create a property called "Unchecked" that negates Checked and bind to that instead.Causerie
How can I pass a negated boolean value without creating another property?Flaring
Yes, but a bind expression is a property, I don't think you can.Causerie
@Causerie now it's better explained, thanksFlaring
@Causerie could you write that as an answer?Flaring
F
14

As @DavidG said in the comments, you cannot bind a value, you need to bind a property, and !Checked returns a value, not a property.

So the only way to do this is by creating a negated property.

Flaring answered 9/3, 2020 at 14:22 Comment(3)
Ideally you should have offered me to write up the answer rather than post yourself. Something to consider for next time.Causerie
@Causerie please write it, and I erase my answer.Flaring
It doesn't matter now, I'm just saying for future posts.Causerie

© 2022 - 2024 — McMap. All rights reserved.