Bind to Window.Current.Bounds.Width in XAML

I have a popup menu on a LayoutAware page.

I really want the popup to fill the screen.

I think the solution is to use Window.Current.Bounds.Height / Width to set the appropriate properties in the grid inside the popup control.

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

Can I do it?

Is there a better way to make a popup populate a screen?

+4
source share
2 answers

You can do this by writing converters in 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 to the resources section of the page -

  <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> 
+5
source

You can use a converter (see Typist) OR use a static class.

In your App.xaml:

 <datamodel:Foo x:Name="FooClass" /> xmlns:datamodel="using:MyProject.Foo.DataModel" 

And in your xaml:

 Source="{Binding Source={StaticResource FooClass}, Path=Width}" 

Where Width is a property in your class that returns Window.Current.Bounds.Width.

Example: public double Width{get{return Window.Current.Bounds.Width;}}

Sincerely.

+4
source

Source: https://habr.com/ru/post/1447132/


All Articles