Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind to Window.Current.Bounds.Width in XAML

I have a popup control on a LayoutAware page.

What I really want is for the popup to fill the screen.

I think that the solution is to use Window.Current.Bounds.Height/Width to set the respective properties on a grid inside of the popup control.

I do not want to use the code behind file to set these properties. I would like to be able to bind to Window.Current.Bounds.Height in the XAML.

Can I do this?

Is there a better way to make a popup fill the screen?

like image 979
Robert Avatar asked Jan 15 '23 23:01

Robert


1 Answers

You can do it by writing converters for height and width.

public class WidthConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return Window.Current.Bounds.Width;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

public class HeightConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        return Window.Current.Bounds.Height;
    }

    public object ConvertBack(object value, Type targetType, object parameter, string language)
    {
        throw new NotImplementedException();
    }
}

Add this in your page resources section -

    <common:WidthConverter x:Key="wc" />
    <common:HeightConverter x:Key="hc" />

Use them for your popup -

        <Popup x:Name="myPopup"  >
            <Grid  Background="#FFE5E5E5" Height="{Binding Converter={StaticResource hc}}" Width="{Binding Converter={StaticResource wc}}" />
        </Popup>
like image 156
Typist Avatar answered Feb 28 '23 14:02

Typist