-
Notifications
You must be signed in to change notification settings - Fork 111
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
base: master
Are you sure you want to change the base?
Conversation
<StackPanel.ContextFlyout> | ||
<MenuFlyout> | ||
<MenuFlyout.Items> | ||
<MenuFlyoutItem Command="{Binding DataContext.RemoveItemCommand, ElementName=page}" |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See my answer here: https://github.com/unoplatform/Uno.Samples/pull/681/files#r1597206927
.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) |
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uno issue: unoplatform/uno#16940
<!-- The Command bind doesn't work on Uno --> | ||
<MenuFlyoutItem Command="{Binding DataContext.RemoveItemCommand, ElementName=page}" |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should indeed, yes.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The issue -> unoplatform/uno#16743
@agneszitte @jeromelaban
There was a problem hiding this comment.
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!
public class Item | ||
{ | ||
public string? Text { get; set; } | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public class Item | |
{ | |
public string? Text { get; set; } | |
} | |
public record Item(string? Text); |
There was a problem hiding this comment.
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/Styles/MaterialFontsOverride.cs
Outdated
Show resolved
Hide resolved
<!-- 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}" |
There was a problem hiding this comment.
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?
This may be unblocked now that unoplatform/uno#17645 was merged. |
GH Issue: #679
Add XAML and C# Markup samples to showcase how to use Binding expressions inside an ItemTemplate.