question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use x:Bind with Converter in Window

See original GitHub issue

Describe the bug Hi, I’ve tried to use the x:Bind with IValueConverter. But the project doesn’t compile - CS1503 Argument 1: cannot convert from 'TestApp.MainWindow' to 'Microsoft.UI.Xaml.FrameworkElement'. Please take a look at the following code. What I do wrong?

Steps to reproduce the bug

<Window
    x:Class="TestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:TestApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">
    <Grid RowDefinitions="Auto, *" x:Name="root">
        <Grid.Resources>
            <local:MyConverter x:Key="MyConverter"/>
        </Grid.Resources>
        <StackPanel Spacing="12"
                    Grid.Row="1">
            <TextBlock Text="{x:Bind MyProperty, Converter={StaticResource MyConverter}}"/>
        </StackPanel>
    </Grid>
</Window>
    public class MyConverter : IValueConverter {
        public object Convert(object value, Type targetType, object parameter, string language) => value;
        public object ConvertBack(object value, Type targetType, object parameter, string language) => value;
    }

    public sealed partial class MainWindow : Window {
        public MainWindow() {
            this.InitializeComponent();            
        }
        public string MyProperty { get; } = "Test";

Expected behavior

Screenshots image

Version Info

NuGet package version:

[WinUI 3 - Project Reunion 0.5: 0.5.6]

Windows app type:

UWP Win32
Yes
Windows 10 version Saw the problem?
Insider Build (xxxxx)
October 2020 Update (19042) Yes
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 Yes
Xbox
Surface Hub
IoT

Additional context

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:11
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
qJakecommented, Sep 11, 2021

Coming from a strictly WPF/.NET Framework background, and trying out WinUI 3.x for the first time… this is really non-intuitive, especially when trying to port multi-window applications.

I realize that there is a fundamental difference in how UWP apps behave versus how traditional multi-window applications behave, but there should be an elegant solution to this problem nonetheless.

11reactions
michael-hawkercommented, Jul 20, 2021

Yeah, I too keep getting tripped up that Window isn’t a DependencyObject… it’s a bit weird.

If the expectation that Window is supposed to just be the host like the implicit Frame created in App.xaml.cs for UWP apps, maybe the File -> New template for WinUI 3 should separate out the MainWindow as also having a MainPage class hosted by it already setup?

This would help lead developers to the proper constructs when building their app and set them up for success vs. having everyone start dumping UI in MainWindow and then getting frustrated.

I mean I really like how the MainWindow is more exposed compared to all the extra logic on Startup that was done in the UWP space, but it’s super confusing and bare-bones template when trying to start building an app from afterwards without creating a new Page first.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Binding to current DataContext with Converter using x:Bind
I don't think it is a good idea to use x:bind with converter in general, the goal of using x:bing is to increase...
Read more >
{x:Bind} markup extension - UWP applications
The xBind markup extension is a high performance alternative to Binding. xBind - new for Windows 10 - runs in less time and...
Read more >
Has x:bind in WinUI 3 effectively killed commanding and ...
I know commanding and converters have been a staple of MVVM pattern and XAML interfaces, but it seems like WinUI 3 is moving...
Read more >
Troubles with converters and x:Bind in UWP – Bruno Sonnino
I was working with the WPF project, converted to UWP in this article, in order to check usage of the new MVVM toolkit,...
Read more >
Binding with x:Bind | Modernizing Your Windows ...
You cannot catch binding errors until an application is running. If you want to change the way data is displayed in a UI,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found