0

I am binding the height of a CustomControl (Let's call it MyButton) to an inheritable attached property inside the style of the CustomControl:

<Setter Property="Height" Value="{Binding (local:Element.IconContainerSize), RelativeSource={RelativeSource Self}}"/>

However, this doesn't work both in the designer and when I run it. This happens only when I set the Bindings inside the style of the CustomControl. When I set the bindings inside the resources of its container or explicitly set it to its Height when declared, it works:

<StackPanel my:Element.IconContainerSize="18">
    <StackPanel.Resources>
        <Style TargetType="{x:Type cc:MyButton}">
            <!-- Works -->
            <Setter Property="Height" Value="{Binding (my:Element.IconContainerSize), RelativeSource={RelativeSource Self}}"/>
        </style>
    </StackPanel.Resources>
    <!-- Also works -->
    <cc:MyButton Height="{Binding (my:Element.IconContainerSize), RelativeSource={RelativeSource Self}}"/>

    <!-- Doesn't work (assuming the style above is removed in this case) -->
    <cc:MyButton />
</StackPanel>


UPDATE: Adding a Path= before the attached property path works only at runtime and not at design time until I manually reload the solution. I can't manage to make it work during Design time right after I rebuild the project for the CustomControl library when I edit something in the project.

DaaWaan
  • 561
  • 4
  • 20

1 Answers1

0

Try setting Path inside the binding: {Binding Path=(my:Element.IconContainerSize), RelativeSource={RelativeSource Self}}. Code the same but sometimes helps.

Drreamer
  • 332
  • 1
  • 10
  • It works atleast when I run it, but it doesn't update in the designer until I manually restart it. – DaaWaan May 12 '20 at 03:30
  • Sorry, I do not have a big experinece in working with XAML designer, thus I cannot suggest anything to you. I have only noticed a thread where similar problem were discussed: https://stackoverflow.com/questions/43429308/dependency-property-not-updating-visual-studio-designer. I do not know whether solutions suggested there can help you or not. – Drreamer May 12 '20 at 11:16