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

x:Bind Function with Boolean return can't be bound to Visibility property #5514

Open
michael-hawker opened this issue Jul 20, 2021 · 6 comments
Labels
area-Binding bug Something isn't working feature proposal New feature proposal product-winui3 WinUI 3 issues team-Markup Issue for the Markup team tempo Issue seen in Tempo app wct

Comments

@michael-hawker
Copy link
Collaborator

Describe the bug
If you have a function you can use with x:Bind that returns a bool, it can't also be used for a property expecting a Visibility type. This works fine when binding a bool based property to a Visibility type with x:Bind, but not with a function. Functions are missing the implicit type conversion step done elsewhere for XAML. 🙁

This means that we have to duplicate our function to work for both boolean and Visibility based properties. 🙁

It'd be great for the x:Bind system to be able to do the same implicit type conversions done elsewhere with properties.

Steps to reproduce the bug
You can see in the following example that a bool value can be bound directly to the visibility of an item (the green rectangle) as well as negated with a function and bound to a bool based property (checkbox).

However, trying to use that same function to bind to a Visibility based property results in an error about types:

    <StackPanel
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Orientation="Vertical"
        Spacing="8">
        <TextBlock>
            <Run Text="Boolean Value: " />
            <Run Text="{x:Bind MyBoolean}" />
        </TextBlock>

        <TextBlock Text="Click button to toggle" />
        <Button x:Name="myButton" Click="myButton_Click">Click Me</Button>

        <TextBlock Text="Checkbox is bound to a function to negate the value:" />
        <CheckBox IsChecked="{x:Bind local:MainWindow.Negate(MyBoolean)}" IsEnabled="False" />

        <Rectangle
            Width="200"
            Height="100"
            Fill="Green"
            Visibility="{x:Bind MyBoolean}" />
        <!--  Return type of 'Negate' must be of type 'Visibility'.  -->
        <!--
        <Rectangle Visibility="{x:Bind local:MainWindow.Negate(MyBoolean)}" Width="200" Height="100" Fill="Red"/>
        -->
    </StackPanel>
    public sealed partial class MainWindow : Window, INotifyPropertyChanged
    {
        private bool _value;
        public bool MyBoolean
        {
            get { return _value; }
            set {
                _value = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(MyBoolean)));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public MainWindow()
        {
            this.InitializeComponent();
        }

        private void myButton_Click(object sender, RoutedEventArgs e)
        {
            MyBoolean = !MyBoolean;
        }

        public static bool Negate(bool value)
        {
            return !value;
        }
    }

Expected behavior
Able to use the same bool based function with x:Bind for Visibility based types. Ideally, any implicit conversions between types can be performed.

Because of this issue it means you have to create functions explicitly for both boolean values and Visibility values even if the logic and input to that function is the same.

Version Info
The example above is with the Windows App SDK 0.8.1, but this is also a problem in WUX based OS xaml. I tested with WinUI 3 to ensure that it wasn't fixed already there, but the problem remains. 🙁

Windows app type:

UWP Win32
Yes Yes
Windows 10 version Saw the problem?
21H1 Build (19043) Yes
October 2020 Update (19042)
May 2020 Update (19041)
November 2019 Update (18363)
May 2019 Update (18362)
October 2018 Update (17763)
April 2018 Update (17134)
Fall Creators Update (16299)
Creators Update (15063)
Device form factor Saw the problem?
Desktop
Xbox
Surface Hub
IoT

Additional context
Related to:

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Jul 20, 2021
@StephenLPeters StephenLPeters added area-Binding needs-winui-3 Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3) product-winui3 WinUI 3 issues team-Markup Issue for the Markup team labels Jul 20, 2021
@StephenLPeters
Copy link
Contributor

@RealTommyKlein FYI

@julianxhokaxhiu
Copy link

This is still an issue today. Any progress on that regard?

@hawkerm
Copy link

hawkerm commented Sep 9, 2022

Encountered this again today, as it's not just static functions but any functions.

One of the most common things to do is inverting a bool in order to show something when a value is false.

Ideally, one could just do this:

<Border Visibility="{x:Bind MyBoolProperty.Equals(x:False), Mode=OneWay}"/>

And be done, but with this bug, you now need a custom converter, or a custom function to return specifically a Visibility value. ☹

@MikeHillberg MikeHillberg added the tempo Issue seen in Tempo app label Sep 9, 2022
@bpulliam bpulliam removed the needs-triage Issue needs to be triaged by the area owners label Dec 6, 2022
@evelynwu-msft evelynwu-msft added feature proposal New feature proposal and removed needs-winui-3 Indicates that feature can only be done in WinUI 3.0 or beyond. (needs winui 3) labels Jul 11, 2023
@michael-hawker
Copy link
Collaborator Author

Bump

@duncanmacmichael duncanmacmichael added the bug Something isn't working label Oct 31, 2023
@Jay-o-Way

This comment has been minimized.

@hawkerm
Copy link

hawkerm commented Jan 5, 2024

@Jay-o-Way no, the built-in converter is only for properties, this bug is about the second part of the doc note about it not working automatically with functions that return a bool:

can bind a Visibility property to a Boolean without creating a converter. Note that this is not a feature of function binding, only property binding.

I imagine this should just be a matter of the x:Bind generator detecting a boolean return type of a function bound to a Visibility parameter and applying the same logic it does for properties?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Binding bug Something isn't working feature proposal New feature proposal product-winui3 WinUI 3 issues team-Markup Issue for the Markup team tempo Issue seen in Tempo app wct
Projects
None yet
Development

No branches or pull requests

9 participants