0

I'm trying to bind my whole window to a underlying usercontrol to let that usercontrol control parent window behaviour. For example i would like to close the parent window from the userControl. I want to create a custom TitleBar that i can reuse in other windows. I have tried using

<views:TitlebarUserCtrl BoundWindow="{Binding ElementName=Window1, Mode=OneWay}" ></views:TitlebarUserCtrl>    

.

public static readonly DependencyProperty BoundCurrentWindow = DependencyProperty.Register("BoundWindow", typeof(Window), typeof(TitlebarUserCtrl), new UIPropertyMetadata(""));
public Window BoundWindow
{
    get
    {
        return (Window)GetValue(BoundCurrentWindow);
    }
    set
    {
        SetValue(BoundCurrentWindow, value);
    }
}

But i only get a error. Any suggestions?

Bjorn
  • 181
  • 2
  • 11
  • 3
    it's not clear what you are trying to achive – Denis Schaf Mar 19 '19 at 12:34
  • 1
    I want to be able to close the window from the usercontrol for example. I want the to be able to call Window.Close() from the usercontrol. – Bjorn Mar 19 '19 at 12:39
  • Which errors? You code does work on my side. The window is bound. By the way - there is a convention to end dependency property with _property_. I.e `BoundWindowProperty` – Rekshino Mar 19 '19 at 12:44
  • The error is: System.Windows.Markup.XamlParseException: ''The invocation of the constructor on type 'HWInterfaceChromaScan.Views.TitlebarUserCtrl' that matches the specified binding constraints threw an exception.' Line number '54' and line position '14'.' TypeInitializationException: The type initializer for 'HWInterfaceChromaScan.Views.TitlebarUserCtrl' threw an exception. ArgumentException: Default value type does not match type of property 'BoundWindow'. – Bjorn Mar 19 '19 at 12:49
  • Have you ever thought about creating your own Window Style / Template, then defining your own custom window control with your own personal hooks / labels / caption properties, etc? Then you can have the visual look applied across the board and your custom window as the baseline for others? – DRapp Mar 19 '19 at 12:55
  • I have, but it becomes overly complicated when using windowchrome and other stuff to get the same sense of feel. This would by far be the most modular and easy solution. If i only got it to work... – Bjorn Mar 19 '19 at 13:01
  • does my suggestion below not work for you? – Denis Schaf Mar 19 '19 at 13:17

2 Answers2

0

You can bind to your window by using a relative source thats looking for a Window-Type-control:

<views:TitlebarUserCtrl BoundWindow="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"/>
Denis Schaf
  • 2,478
  • 1
  • 8
  • 17
  • This works, but so does the xaml i have written above. Your solution is much nicer though. The problem was that i had to specify new UIPropertyMetadata("") to null instead. – Bjorn Mar 20 '19 at 17:39
0

Thanks for all help. I don't know what was not working. I cleared the obj folder and the error dissapeared. Edit: and I set UIPropertyMetadata("") to null - that seemed to fix it.

Here is the correct code:

C#
public static readonly DependencyProperty BoundCurrentWindow = DependencyProperty.Register("BoundWindowProperty", typeof(Window), typeof(TitlebarUserCtrl), null);
public Window BoundWindowProperty
    {
        get
        {
            return (Window)GetValue(BoundCurrentWindow);
        }
        set
        {
            SetValue(BoundCurrentWindow, value);
        }
    }

WPF
<views:TitlebarUserCtrl BoundWindowProperty="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}" />
Bjorn
  • 181
  • 2
  • 11