2

I've faced a problem: Attached property doesn't work for Button's Flayout.

I have an outer button with inner button (flayout), attached property - is a ICommand type property. Outer button successfully binds to attached property BUT inner button doesn't.

Here User Control code:

<UserControl x:Class="uwp_AttachedProperty.MyUserControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="using:uwp_AttachedProperty"
             Name="Root">

    <Button Command="{Binding ElementName=Root, Path=(local:AttachedProps.CommandAdd), Mode=OneWay}">
        <Button.Flyout>
            <Flyout>
                <!--*** DOESN'T WORK HERE***-->
                <Button Command="{Binding ElementName=Root, Path=(local:AttachedProps.CommandAdd),Mode=OneWay}"/>
            </Flyout>
        </Button.Flyout>
    </Button>
</UserControl>

Attached property code:

public sealed class AttachedProps : DependencyObject
    {
        public static readonly DependencyProperty CommandAddProperty = DependencyProperty.Register(
            "CommandAdd",
            typeof(ICommand),
            typeof(AttachedProps),
            new PropertyMetadata(null));

        public static void SetCommandAdd(UIElement element, ICommand value) { element.SetValue(CommandAddProperty, value); }
        public static ICommand GetCommandAdd(UIElement element) { return (ICommand)element.GetValue(CommandAddProperty); }
    }

My case is an ICommand propery, but it doesn't work any type (double, string etc.) of attached property. Does any one faced the same problem, how can it be fixed?

(Windows 10, 1809, Build: 17763) Thanks.

Dorian
  • 21
  • 1

1 Answers1

0

Attached Property binding not working for Flayout (Xaml, UWP)

Derive this case reply The problem is you used Binding ElementName=Root in the Flyout, When you bind data in the content of Flyout, the binding source is in Page, but the actual binding target is in PopupRoot, they have different DataContext, so can't it work here.

Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • But `MyUserControl` receives value of attached property somewhere outside. If problem in `Binding ElementName=Root` - then `outer button` won't work too, but it works well. Only (inner) Flayout button isn't able to use attached property. That's a problem. – Dorian Oct 01 '19 at 06:47
  • Yep, you are correct, the problem is not attached property (i missed parenthesis). – Nico Zhu Oct 01 '19 at 07:05