Skip to content

Commit

Permalink
Merge pull request #14759 from unoplatform/mergify/bp/release/stable/…
Browse files Browse the repository at this point in the history
…5.0/pr-14737

x:Bind to event in `ResourceDictionary` (backport #14737)
  • Loading branch information
jeromelaban authored Dec 11, 2023
2 parents 028a4bc + 630bee8 commit d108af5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ private SourceText InnerGenerateFile()
if (topLevelControl.Type.Name == "ResourceDictionary")
{
_isTopLevelDictionary = true;
_xClassName = FindClassName(topLevelControl);

using (TrySetDefaultBindMode(topLevelControl))
{
Expand Down
2 changes: 2 additions & 0 deletions src/Uno.UI.Tests/App/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="using:Uno.UI.Tests.App.Views"
xmlns:local="using:Uno.UI.Tests.App.Xaml"
xmlns:xbind="using:Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls"
mc:Ignorable="not_win">
<Application.Resources>
<ResourceDictionary>
Expand All @@ -31,6 +32,7 @@
</ResourceDictionary>
<local:Subclassed_Dictionary />
<local:Subclassed_Dictionary_With_Property Test="Test123"/>
<xbind:XBind_ResourceDictionary />
</ResourceDictionary.MergedDictionaries>
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Light">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<ResourceDictionary
x:Class="Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls.XBind_ResourceDictionary"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls">

<!-- Implicitly applied default style -->
<Style BasedOn="{StaticResource DefaultxBindProjectTemplateStyle}" TargetType="controls:XBind_ResourceDictionary_Control" />

<Style x:Key="DefaultxBindProjectTemplateStyle" TargetType="controls:XBind_ResourceDictionary_Control">
<Style.Setters>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:XBind_ResourceDictionary_Control">
<ContentControl
x:Name="ContentContainer"
HorizontalContentAlignment="Stretch"
VerticalContentAlignment="Stretch">
<ContentControl.ContentTemplate>
<DataTemplate x:DataType="controls:XBind_ResourceDictionary_Control">
<Button Content="Hello" Loaded="{x:Bind Element_Loaded}" />
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style.Setters>
</Style>
</ResourceDictionary>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

namespace Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls;
public sealed partial class XBind_ResourceDictionary : ResourceDictionary
{
public XBind_ResourceDictionary()
{
this.InitializeComponent();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace Uno.UI.Tests.Windows_UI_Xaml_Data.xBindTests.Controls;

internal class XBind_ResourceDictionary_Control : Control
{
/// <summary>
/// Creates a new instance of the <see cref="ProjectTemplate_xBind"/> class.
/// </summary>
public XBind_ResourceDictionary_Control()
{
this.DefaultStyleKey = typeof(XBind_ResourceDictionary_Control);

// Allows directly using this control as the x:DataType in the template.
this.DataContext = this;
}

public bool ElementLoadedInvoked { get; private set; }

public void Element_Loaded(object sender, RoutedEventArgs args)
{
ElementLoadedInvoked = true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1461,6 +1461,15 @@ public void When_Indexer_Then_Property_Access_Update_Collection_Element()
Assert.AreEqual("Updated2", SUT.tbDict2.Text);
}

[TestMethod]
public void When_XBind_In_ResourceDictionary()
{
var SUT = new XBind_ResourceDictionary_Control();
SUT.ForceLoaded();

Assert.IsTrue(SUT.ElementLoadedInvoked);
}

private async Task AssertIsNullAsync<T>(Func<T> getter, TimeSpan? timeout = null) where T : class
{
timeout ??= TimeSpan.FromSeconds(1);
Expand Down

0 comments on commit d108af5

Please sign in to comment.