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

feat: Binding Samples #681

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft

Conversation

eriklimakc
Copy link
Contributor

GH Issue: #679

Add XAML and C# Markup samples to showcase how to use Binding expressions inside an ItemTemplate.

<StackPanel.ContextFlyout>
<MenuFlyout>
<MenuFlyout.Items>
<MenuFlyoutItem Command="{Binding DataContext.RemoveItemCommand, ElementName=page}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using {utu:ItemsControlBinding Path=DataContext.RemoveItemCommand} here won't work. I guess because it's inside a MenuFlyout the DataContext gets messed up.

image

@kazo0 @dansiegel

Copy link
Contributor

Choose a reason for hiding this comment

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

Comment on lines 47 to 51
.Content("Delete")
// RelativeSource as TemplatedParent works on Windows but not on Uno
// RelativeSource as Self works on Uno but not on Windows
.CommandParameter(x => x.RelativeSource<Button>(RelativeSourceMode.Self)
.Binding(y => y.DataContext)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we have a situation here. Uno and Windows behave differently. To have it working on Windows we need to use RelativeSourceMode.TemplatedParent and on Uno RelativeSourceMode.Self.

@dansiegel @Xiaoy312

Copy link
Contributor

Choose a reason for hiding this comment

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

we should get TemplatedPArent fixed in uno then~ (sounds like unoplatform/uno#12732)

Comment on lines 34 to 35
<!-- The Command bind doesn't work on Uno -->
<MenuFlyoutItem Command="{Binding DataContext.RemoveItemCommand, ElementName=page}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Binding the command like this doesn't work on Uno. Any ideas?

@agneszitte @dansiegel @kazo0 @Xiaoy312

Copy link
Contributor

Choose a reason for hiding this comment

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

@eriklimakc It is normal, it is because your ElementName=page is outside the DataTemplate where you are trying to bind the value here. Should not work on Windows also in that case with the current code if I am not mistaken.
That is why we created the AncestorBinding & ItemsControlBinding in Uno.Toolkit.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huum, that's strange.

Command="{Binding DataContext.RemoveItemCommand, ElementName=page}" works on Windows.

And as mentioned here #681 (comment) using AncestorBinding & ItemsControlBinding as {utu:ItemsControlBinding Path=DataContext.RemoveItemCommand} or {utu:AncestorBinding AncestorType=ItemsControl, Path=DataContext.RemoveItemCommand} doesn't work.

Maybe because it's inside a MenuFlyout that is inside a DataTemplate?

Copy link
Contributor

Choose a reason for hiding this comment

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

yea... flyout content is in a separate visual tree branch which is why ancestor/itemscontrol-binding wouldnt work here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Xiaoy312 is there another way of making that Binding work for that case?

Copy link
Contributor

Choose a reason for hiding this comment

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

@jeromelaban, @MartinZikmund
If Command="{Binding DataContext.RemoveItemCommand, ElementName=page}" works on Windows inside the MenuFlyout than we would need to match that behavior on Uno right?

Related code:

<MenuFlyoutItem Command="{Binding DataContext.RemoveItemCommand, ElementName=page}"

Copy link
Member

Choose a reason for hiding this comment

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

It should indeed, yes.

Copy link
Contributor

Choose a reason for hiding this comment

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

@eriklimakc If you can create a small repro and open an issue in Uno for this one, please

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you a lot for the issue @eriklimakc!

UI/BindingSources/CSHARP/Binding/App.xaml.cs Outdated Show resolved Hide resolved
UI/BindingSources/CSHARP/Binding/Binding.csproj Outdated Show resolved Hide resolved
UI/BindingSources/CSHARP/Binding/Binding.csproj Outdated Show resolved Hide resolved
UI/BindingSources/CSHARP/Binding/MainPage.cs Outdated Show resolved Hide resolved
UI/BindingSources/CSHARP/Binding/MainViewModel.cs Outdated Show resolved Hide resolved
Comment on lines 49 to 52
public class Item
{
public string? Text { get; set; }
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
public class Item
{
public string? Text { get; set; }
}
public record Item(string? Text);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can't use it on Windows-XAML, getting the following error:

1>C:\UnoPlatform\Samples\Uno.Samples\UI\BindingSources\XAML\Binding\obj\Debug\net8.0-windows10.0.19041\XamlTypeInfo.g.cs(711,13,711,22): error CS8852: Init-only property or indexer 'Item.Text' can only be assigned in an object initializer, or on 'this' or 'base' in an instance constructor or an 'init' accessor.

UI/BindingSources/CSHARP/Binding/MainViewModel.cs Outdated Show resolved Hide resolved
UI/BindingSources/CSHARP/Binding/ReadMe.md Outdated Show resolved Hide resolved
UI/BindingSources/XAML/Binding/App.xaml Outdated Show resolved Hide resolved
@eriklimakc eriklimakc marked this pull request as draft May 14, 2024 17:28
Comment on lines +34 to +39
<!-- Using AcestorBinding inside the Flyout doesn't work -->
<!-- https://github.com/unoplatform/uno.toolkit.ui/issues/1135 -->

<!-- Using ElementName on Uno doesn't work -->
<!-- https://github.com/unoplatform/uno/issues/16743 -->
<MenuFlyoutItem Command="{Binding DataContext.RemoveItemCommand, ElementName=page}"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we then try to use something else? Maybe having the MainViewModel as a StaticResource and setting it as the source instead?

@dansiegel @jeromelaban @Xiaoy312 @agneszitte

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Quick samples in both XAML and C# Markup that shows how to call commands + parameters inside an ItemTemplate
6 participants