David
David

Reputation: 537

WPF/XAML – Binding of WindowChrome.CaptionHeight

Theme/Generic.xaml of my WPF Custom Control MyBaseWindow looks like this:

<Style TargetType="{x:Type MyBaseWindowPath:MyBaseWindow}">
        …
        <Setter Property="WindowChrome.WindowChrome">
            <Setter.Value>
                <WindowChrome CaptionHeight="{Binding ActualHeight, ElementName=HeaderPanel}"
                              GlassFrameThickness="0"
                              CornerRadius="0" />
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type MyBaseWindowPath:MyBaseWindow}">
                    …
                    <DockPanel x:Name="HeaderPanel">
                    …
                    </DockPanel>
                    …
                </ControlTemplate>
            </Setter.Value>
        </Setter>
</Style>

I would like to bind ActualHeight property of HeaderPanel element within ControlTemplate. Is it possible?


Another solution is use fixed height for WindowChrome.CaptionHeight and HeaderPanel.Height, but I prefer binding to exact height.

Upvotes: 1

Views: 1647

Answers (2)

BlackNoizE
BlackNoizE

Reputation: 1

In 2021, my solution without binding was to assign a x:Name to the WindowChrome and change CaptionHeight value in Window_SizeChanged event:

private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
    SessionWindowChrome.CaptionHeight = ActualHeight
       - SessionWindowChrome.ResizeBorderThickness.Top 
       - SessionWindowChrome.ResizeBorderThickness.Bottom;    
}

Take in account that i use ResizeBorderThickness variables too...

Upvotes: 0

Berin Loritsch
Berin Loritsch

Reputation: 11463

ActualHeight is not a DependencyProperty or an INotifyPropertyChanged, so you can't bind directly to it and expect more than a one-time read. You can set a trigger on it in a style.

Work-around includes:

  • Creating a property in your class (view side) that is either a DependencyProperty or an INotifyPropertyChanged property.
  • Registering for the SizeChanged event on the element you want to know the AbsoluteHeight on.
  • In the handler for the event, update the property you created.

A fancier implementation would use attached dependency properties that essentially do the same thing, but they would be a bit more general purpose.

Upvotes: 1

Related Questions