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

Can't set x:Bind.Converter to StaticResource in a Window #6369

Closed
2 tasks
MikeHillberg opened this issue Nov 23, 2021 · 3 comments
Closed
2 tasks

Can't set x:Bind.Converter to StaticResource in a Window #6369

MikeHillberg opened this issue Nov 23, 2021 · 3 comments
Labels
area-Binding duplicate This issue or pull request already exists team-Markup Issue for the Markup team

Comments

@MikeHillberg
Copy link
Contributor

Describe the bug

Window doesn't have a Resources property, so you set resources typically in the root of the Window content. If you try to use that in an x:Bind Converter using a {StaticResource}, it fails in the generated x:Bind code on the SetConverterLookupRoot call with the error:

Argument 1: cannot convert from 'App5.MainWindow' to 'Microsoft.UI.Xaml.FrameworkElement'

public global::Microsoft.UI.Xaml.Markup.IComponentConnector GetBindingConnector(int connectionId, object target)
{
    global::Microsoft.UI.Xaml.Markup.IComponentConnector returnValue = null;
    switch(connectionId)
    {
    case 1: // MainWindow.xaml line 1
        {                    
            global::Microsoft.UI.Xaml.Window element1 = (global::Microsoft.UI.Xaml.Window)target;
            MainWindow_obj1_Bindings bindings = new MainWindow_obj1_Bindings();
            returnValue = bindings;
            bindings.SetDataRoot(this);
            bindings.SetConverterLookupRoot(this); // <=== Error
            this.Bindings = bindings;
            element1.Activated += bindings.Activated;
        }
        break;
    }
    return returnValue;
}

See also #3442, where were it not for this compiler error, the scenario still wouldn't work for a different issue.

Steps to reproduce the bug

<Window
    x:Class="App5.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:App5">
    
    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <StackPanel.Resources>
            <local:MyConverter x:Key="myConverter"/>
        </StackPanel.Resources>
        
        <TextBlock Text="{x:Bind TestProp, Converter={StaticResource myConverter}}"/>
    </StackPanel>
    
</Window>
public class MyConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return "World";
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

Expected behavior

No response

Screenshots

No response

NuGet package version

No response

Windows app type

  • UWP
  • Win32

Device form factor

No response

Windows version

No response

Additional context

No response

@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Nov 23, 2021
@hawkerm
Copy link

hawkerm commented Feb 14, 2022

Duplicate of #4966?

@StephenLPeters
Copy link
Contributor

Looks like it to me. @RealTommyKlein if you agree can you close this.

@StephenLPeters StephenLPeters added area-Binding team-Markup Issue for the Markup team duplicate This issue or pull request already exists labels Feb 16, 2022
@krschau
Copy link
Contributor

krschau commented Jun 22, 2022

Duplicate of #4966

@krschau krschau marked this as a duplicate of #4966 Jun 22, 2022
@krschau krschau closed this as completed Jun 22, 2022
@ghost ghost removed the needs-triage Issue needs to be triaged by the area owners label Jun 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Binding duplicate This issue or pull request already exists team-Markup Issue for the Markup team
Projects
None yet
Development

No branches or pull requests

4 participants