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

x:Bind to event in ResourceDictionary #14737

Merged
merged 6 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,7 @@ private SourceText InnerGenerateFile()
if (topLevelControl.Type.Name == "ResourceDictionary")
{
_isTopLevelDictionary = true;
_xClassName = FindClassName(topLevelControl);

using (TrySetDefaultBindMode(topLevelControl))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,43 @@ public async Task When_Content_Set_Null_ComboBox()
Assert.IsTrue(comboBox.SelectedIndex == -1);
}

#if HAS_UNO
[TestMethod]
[RunsOnUIThread]
public async Task When_FindName_ContentControl_Without_ContentTemplate()
{
var sut = new ContentControl
{
Width = 100,
Height = 100,
Content = new TextBox()
};

WindowHelper.WindowContent = sut;
await WindowHelper.WaitForLoaded(sut);

Assert.IsNotNull(sut.FindName("ContentElement"));
}

[TestMethod]
[RunsOnUIThread]
public async Task When_FindName_ContentControl_With_ContentTemplate()
{
var sut = new ContentControl
{
Width = 100,
Height = 100,
Content = new TextBox(),
ContentTemplate = new DataTemplate(() => new TextBlock())
};

WindowHelper.WindowContent = sut;
await WindowHelper.WaitForLoaded(sut);

Assert.IsNull(sut.FindName("ContentElement"));
}
#endif

private class SignInViewModel
{
public string UserName { get; set; } = "Steve";
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
14 changes: 11 additions & 3 deletions src/Uno.UI/UI/Xaml/IFrameworkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,17 @@ public static IFrameworkElement FindName(IFrameworkElement e, ViewGroup group, s

// If element is a ContentControl with a view as Content, include the view and its children in the search,
// to better match Windows behaviour
var content =
(e as ContentControl)?.Content as IFrameworkElement ??
(e as Controls.Primitives.Popup)?.Child as IFrameworkElement;
IFrameworkElement content = null;
if (e is ContentControl contentControl &&
contentControl.Content is IFrameworkElement innerContent &&
contentControl.ContentTemplate is null) // Only include the Content view if there is no ContentTemplate.
{
content = innerContent;
}
else if (e is Controls.Primitives.Popup popup)
{
content = popup.Child as IFrameworkElement;
}

if (content != null)
{
Expand Down