microsoft / microsoft-ui-xaml

WinUI: a modern UI framework with a rich set of controls and styles to build dynamic and high-performing Windows applications.
MIT License
6.37k stars 682 forks source link

[xBind] unexpected generated code when using a control name inside a function binding inside datatemplate #10161

Open mouhamedhazem149 opened 1 week ago

mouhamedhazem149 commented 1 week ago

Describe the bug

using an element property as an argument for function binding inside template binding (testTextBox.Text in the following example)

<StackPanel
        HorizontalAlignment="Center"
        VerticalAlignment="Center"
        Orientation="Vertical">
        <StackPanel.Resources>
            <DataTemplate x:Key="TestDataTemplate" x:DataType="x:String">
                <TextBlock Text="{x:Bind local:TestControl.ToTestFunc(testTextBox.Text), Mode=OneWay}" />
            </DataTemplate>
        </StackPanel.Resources>
        <TextBox x:Name="testTextBox" Text="Duplication" />

        <ContentControl Content="Test" ContentTemplate="{StaticResource TestDataTemplate}" />
    </StackPanel>

the generated g.cs file has an unexpected generated code


            private bool TryGet_testTextBox(out global::Microsoft.UI.Xaml.Controls.TextBox val)
            {
                global::System.String obj;
                if (TryGet_(out obj) && obj != null)
                {
                    val = bindings.obj4;
                    return true;
                }
                else
                {
                    val = default(global::Microsoft.UI.Xaml.Controls.TextBox);
                    return false;
                }
            }

            private bool TryGet_(out global::System.String val)
            {
                val = this;
                return true;
            }

. correcting the generated code for TryGet_testTextBox

val = bindings.obj4; to val = obj4;

and TryGet_

val = this; to val = this.dataRoot;

is the expected code. which works fine after testing

Steps to reproduce the bug

use an element name in function binding inside a datatemplate

Expected behavior

No response

Screenshots

No response

NuGet package version

WinUI 3 - Windows App SDK 1.6.2: 1.6.241106002

Windows version

No response

Additional context

the only work around i found is to avoid the function binding

for the mentioned example above,

<DataTemplate x:Key="TestDataTemplate" x:DataType="x:String">
    <TextBlock Text="{x:Bind decoyResource.Value, Mode=OneWay}">
        <TextBlock.Resources>
            <local:Decoy x:Name="decoyResource" Value="{x:Bind testTextBox.Text, Mode=OneWay}" />
        </TextBlock.Resources>
    </TextBlock>
</DataTemplate>

public class Decoy : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private string _value;
    public string Value
    {
        get => _value;
        set
        {
            if (_value != value)
            {
                _value = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value)));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Result)));
            }
        }
    }

    public string Result => Value;
}

min repro project link