Skip to content

Allow specifying BindingMode in AncestorBinding #1384

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions src/Uno.Toolkit.RuntimeTests/Tests/AncestorBindingTests.cs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the newly added doesnt verify the nature of the binding (being two-ways)
you should either retrieve the binding on the property and assert its mode, or
change the destination value and validate if the source value is updated as a result
preferably both

Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,20 @@ public async Task Ancestor_Nested_PageBinding()
Assert.AreEqual(sut.Text, setup.Tag);
}

[TestMethod]
public async Task Ancestor_TwoWay_Nested_PageBinding()
{
var setup = new AncestorBindingTest();
await UnitTestUIContentHelperEx.SetContentAndWait(setup);

var lv = setup.GetFirstDescendant<ListView>(x => x.Name == "TopLevelListView") ?? throw new Exception("Failed to find TopLevelListView");
var container = lv.ContainerFromIndex(0);
var sut = (container as FrameworkElement)?.GetFirstDescendant<TextBlock>(x => x.Name == "NestedLvTextBlock1TwoWay") ?? throw new Exception("Failed to find NestedLvTextBlock1TwoWay");
sut.Text = "NestedLvTextBlock1TwoWayTag";

Assert.AreEqual(sut.Text, setup.Tag);
}

[TestMethod]
public async Task Ancestor_Nested_LvBinding()
{
Expand All @@ -59,6 +73,20 @@ public async Task Ancestor_Nested_LvBinding()
Assert.AreEqual(sut.Text, lv.Tag);
}

[TestMethod]
public async Task Ancestor_TwoWay_Nested_LvBinding()
{
var setup = new AncestorBindingTest();
await UnitTestUIContentHelperEx.SetContentAndWait(setup);

var lv = setup.GetFirstDescendant<ListView>(x => x.Name == "TopLevelListView") ?? throw new Exception("Failed to find TopLevelListView");
var container = lv.ContainerFromIndex(0);
var sut = (container as FrameworkElement)?.GetFirstDescendant<TextBlock>(x => x.Name == "NestedLvTextBlock2") ?? throw new Exception("Failed to find NestedLvTextBlock2");
sut.Text = "NestedLvTextBlock1TwoWayTag";

Assert.AreEqual(sut.Text, lv.Tag);
}

[TestMethod]
public async Task Ancestor_Converter_InitialValue()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
<StackPanel>
<TextBlock x:Name="NestedLvTextBlock1" Text="{utu:AncestorBinding AncestorType=Page, Path=Tag}" />
<TextBlock x:Name="NestedLvTextBlock2" Text="{utu:ItemsControlBindingExtension Path=Tag}" />
<TextBlock x:Name="NestedLvTextBlock1TwoWay" Text="{utu:AncestorBinding AncestorType=Page, Path=Tag, Mode=TwoWay}" />
<TextBlock x:Name="NestedLvTextBlock2TwoWay" Text="{utu:ItemsControlBindingExtension Path=Tag, Mode=TwoWay}" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
Expand Down
7 changes: 6 additions & 1 deletion src/Uno.Toolkit.UI/Markup/AncestorBindingExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public class AncestorBindingExtension : MarkupExtension
/// </summary>
public string? ConverterLanguage { get; set; }

/// <summary>
/// Gets or sets a value that indicates the direction of the data flow in the binding.
/// </summary>
public BindingMode Mode { get; set; } = BindingMode.OneWay;

public AncestorBindingExtension()
{
}
Expand Down Expand Up @@ -96,7 +101,7 @@ void OnTargetLoaded(object s, RoutedEventArgs e)
{
Path = new PropertyPath(Path),
Source = source,
Mode = BindingMode.OneWay,
Mode = Mode,
Converter = Converter,
ConverterLanguage = ConverterLanguage,
ConverterParameter = ConverterParameter,
Expand Down
Loading