data binding - UWP - bind element to main window size AND also update the value as window size changes -


i want bind text block current size of window. in current implementation size of main window set @ run time if resize window after application has launched new size not updated in text block.

<grid x:name="grid" background="#ffe8e8e8">    <textbox x:name="textboxsample" width="300" height="200" text="{binding actualwidth, elementname=grid}"></textbox> </grid> 

in uwp, grid controls automatically resize fit parent container.

your textbox has set height , width, prevent resizing when parent grid resized.

in scenario described, workaround i've implemented adding screenheight , screenwidth properties view model updated when screen size changed. then, can bind height/width of whatever control wanting resized properies. here sample implementation:

your xaml file:

<page x:name="mainpage"     x:class="yourapp.mainpage"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:local="using:yourapp"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     xmlns:vm="using:yourapp.viewmodels"     sizechanged="mainpage_sizechanged">     <page.datacontext>         <vm:viewmodel x:name="datacontext"/>     </page.datacontext>     <yourcontrol height="{binding screenheight}" width="{binding screenwidth}"/> </page> 

your viewmodel

public class viewmodel: inotifypropertychanged {     private double _screenwidth;     private double _screenheight;      public double screenwidth { { return _screenwidth; } set { _screenwidth = value; onpropertychanged("screenwidth"); } }     public double screenheight { { return _screenheight; } set { _screenheight = value; onpropertychanged("screenheight"); } }      public event propertychangedeventhandler propertychanged;      protected void onpropertychanged(string name)     {         propertychangedeventhandler handler = propertychanged;         if (handler != null)         {             handler(this, new propertychangedeventargs(name));         }     } } 

your code behind

private void mainpage_sizechanged(object sender, sizechangedeventargs e) {     datacontext.screenheight = this.actualheight;     datacontext.screenwidth = this.actualwidth; } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -