Roest
Topic Author
Posts: 35
Joined: 15 Jan 2020, 15:30

Access window size in xaml or data model

24 Feb 2020, 14:56

When resizing the window we have to do something like
m_view->SetSize( this->width(), this->height() );
Is it possible for a xaml or a data model to access these values?

Tried something like
"{Binding Source={x:Static SystemParameters.PrimaryScreenWidth}}"
as found on StackOverflow but that's probably just WPF like.

Storing the pointer to the data model somewhere global works but doesn't feel elegant.
 
User avatar
sfernandez
Site Admin
Posts: 2984
Joined: 22 Dec 2011, 19:20

Re: Access window size in xaml or data model

25 Feb 2020, 13:02

Xaml root element fills by default the avaliable space specified in view->SetSize(), so you can bind to root ActualWidth and ActualHeight if you need that info from the xaml:
<Grid
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  x:Name="Root">
  ...
  <Rectangle
    Width="{Binding ActualWidth, ElementName=Root}"
    Height="{Binding ActualHeight, ElementName=Root}" .../>
  ...
</Grid>
Could that work for you?
 
Roest
Topic Author
Posts: 35
Joined: 15 Jan 2020, 15:30

Re: Access window size in xaml or data model

25 Feb 2020, 13:18

That works, thanks. The reason I want this is that I have
<Viewbox>
	<Grid x:Name="GameGrid" Width="{Binding WindowWidth}" Height="{Binding WindowHeight}">
...		
just below the root element. So when these values are set smaller than the actual window size it can be use to scale the entire gui.
So I solved that now by feeding the window size into the view model, multiply it with the scaling factor and supply the binding.

Now that I see that it is possible to get from the xaml I need to find out how to multiply that with the scaling factor.
 
User avatar
jsantos
Site Admin
Posts: 3907
Joined: 20 Jan 2012, 17:18
Contact:

Re: Access window size in xaml or data model

03 Mar 2020, 10:31

Now that I see that it is possible to get from the xaml I need to find out how to multiply that with the scaling factor.
You can use converters for this although I think that the idea of doing the maths in code-behind and exposing them in the data context is not necessarily bad.

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot] and 2 guests