microsoft-ui-xaml
microsoft-ui-xaml copied to clipboard
`x:Load="{x:Bind Blah, Mode=OneWay}"` not working in control template
Describe the bug
x:Bind works in control template since 1809. There is a bug however which prevents x:Load from being used with x:Bind. Note that x:Load="False" work, only x:Load={x:Bind Blah, Mode=OneWay} doesn't.
Steps to reproduce the bug
- Create a blank app, create the following
CustomControlTemplate.xamlandCustomControlTemplate.cs(using user control template -> rename is probably the fastest)
// xaml
<ResourceDictionary
x:Class="XLoadBindingNotWorkingInTemplate.CustomControlTemplate"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:XLoadBindingNotWorkingInTemplate"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Style TargetType="local:CustomControl" BasedOn="{StaticResource DefaultCustomControlStyke}"/>
<Style x:Key="DefaultCustomControlStyke" TargetType="local:CustomControl">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:CustomControl">
<TextBlock x:Name="textBlock" Text="{x:Bind Greeting, Mode=OneWay}" x:Load="{x:Bind IsVisible, Mode=OneWay}" />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
// cs
public sealed partial class CustomControlTemplate : ResourceDictionary
{
public CustomControlTemplate()
{
this.InitializeComponent();
}
}
- Add
CustomControlthat use the style
public sealed class CustomControl : Control, INotifyPropertyChanged
{
public CustomControl()
{
this.DefaultStyleKey = typeof(CustomControl);
}
public event PropertyChangedEventHandler PropertyChanged;
public bool IsVisible { get; private set; }
public string Greeting { get { return "Hello world"; } }
public void MakeVisible()
{
IsVisible = true;
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(IsVisible)));
}
}
- Click run in VS.
- Observe doesn't compile with xaml compiler error.
- Navigate to
objand findCustomControlTemplate.g.cs, notice 2 compiler errors whereobj2is used without being declared
Expected behavior
x:Load="{x:Bind Blah, Mode=OneWay}" works.
Screenshots

NuGet package version
No response
Windows app type
- [X] UWP
- [ ] Win32
Device form factor
No response
Windows version
No response
Additional context
- This is a WUX issue, but it is likely affecting WinUI 3 as well.
- Also reproducible with C++.
- The generated code should use
dataRoot.GetTemplateChildinstead ofobj2.FindName. - Minimal repro: https://github.com/roxk/XLoadBindingNotWorkingInTemplateCs
As a workaround, write x:Load="False" and manually call GetTemplateChild(elementName).
Still an issue in WinUI3 with WASDK 1.8
As a workaround, write
x:Load="False"and manually callGetTemplateChild(elementName).
This is not really a workaround. Unloading a control that is loaded in code-behind could be tricky.