UWP-将元素绑定到主窗口大小,并在窗口大小更改时更新值 [英] UWP - bind element to main window size AND also update the value as window size changes

查看:88
本文介绍了UWP-将元素绑定到主窗口大小,并在窗口大小更改时更新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将当前窗口大小绑定到文本块。
在当前实现中,如果我在应用程序启动后调整窗口的大小,则在运行时设置主窗口的大小,但是在文本块中不会更新新的大小。

I want to bind to a text block the current size of the window. In the current implementation the size of the main window is set at run time BUT if I resize the window after the application has launched the new size is not updated in the text block.

<Grid x:Name="grid" Background="#FFE8E8E8">
   <TextBox x:Name="textBoxSample" Width="300" Height="200" Text="{Binding ActualWidth, ElementName=grid}"></TextBox>
</Grid>


推荐答案

在UWP中,网格控件通常会自动调整大小以适合它的父容器。

In UWP, the Grid controls normally automatically resize to fit its parent container.

您的文本框具有设置的高度和宽度,这将防止在调整其父网格大小时调整其大小。

Your Textbox however has a set Height and Width, this will prevent it from resizing when its parent grid is resized.

在您描述的情况下,我实现的一种解决方法是将ViewHeight和ScreenWidth属性添加到视图模型中,这些属性会在更改屏幕尺寸时更新。然后,您可以将要调整大小的任何控件的高度/宽度绑定到这些属性。这是一个示例实现:

In the scenario you described, a workaround that I've implemented was adding ScreenHeight and ScreenWidth properties to my view model that are updated when the screen size is changed. Then, you can bind the height/width of whatever control you are wanting to be resized to those properies. Here is a sample implementation:

您的XAML文件:

<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>

您的ViewModel

Your ViewModel

public class ViewModel: INotifyPropertyChanged
{
    private double _screenWidth;
    private double _screenHeight;

    public double ScreenWidth { get { return _screenWidth; } set { _screenWidth = value; OnPropertyChanged("ScreenWidth"); } }
    public double ScreenHeight { get { 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));
        }
    }
}

您后面的代码

private void MainPage_SizeChanged(object sender, SizeChangedEventArgs e)
{
    dataContext.ScreenHeight = this.ActualHeight;
    dataContext.ScreenWidth = this.ActualWidth;
}

这篇关于UWP-将元素绑定到主窗口大小,并在窗口大小更改时更新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆