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

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

Open
mouhamedhazem149 opened this issue Nov 13, 2024 · 0 comments
Labels
bug Something isn't working needs-triage Issue needs to be triaged by the area owners

Comments

@mouhamedhazem149
Copy link

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

@mouhamedhazem149 mouhamedhazem149 added the bug Something isn't working label Nov 13, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Nov 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs-triage Issue needs to be triaged by the area owners
Projects
None yet
Development

No branches or pull requests

1 participant