绑定到窗口高度和宽度的问题 [英] Problems with binding to Window Height and Width

查看:90
本文介绍了绑定到窗口高度和宽度的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将窗口的高度和宽度绑定到视图模型中的属性时,我有一些问题。这是一个小的示例应用程序来说明问题。这是app.xaml.xs中的代码

I have some problems when I try to bind the height and width of a window to properties in my view model. Here is a small sample app to illustrate the problem. This is the code in app.xaml.xs

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
       base.OnStartup(e);
        MainWindow mainWindow = new MainWindow();
        MainWindowViewModel mainWindowViewModel = new MainWindowViewModel();
        mainWindow.DataContext = mainWindowViewModel;
        mainWindow.Show();
    }
}

这是MainWindow.xaml:

This is MainWindow.xaml:

<Window x:Class="TestApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Height="{Binding WindowHeight}" 
        Width="{Binding WindowWidth}"
        BorderThickness="{Binding WindowBorderThickness}">
</Window>

这是视图模型:

public class MainWindowViewModel
{
    public int WindowWidth { get { return 100; } }
    public int WindowHeight { get { return 200; } }
    public int WindowBorderThickness { get { return 8; } }
}

当程序启动时,WindowHeight和WindowBorderThickness的getter WindowWidth)被调用,所以窗口的高度和边框设置正确,但不是宽度。

When the program is started the getters of WindowHeight and WindowBorderThickness (but not WindowWidth) are called, so the height and the border of the window is set properly, but not the width.

然后添加将为所有属性触发PropertyChanged的按钮,所以view模型现在看起来像这样:

I then add button that will trigger PropertyChanged for all properties, so that the view model now looks like this:

public class MainWindowViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public void TriggerPropertyChanges()
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs("WindowWidth"));
            PropertyChanged(this, new PropertyChangedEventArgs("WindowHeight"));
            PropertyChanged(this, new PropertyChangedEventArgs("WindowBorderThickness"));
        }

    }

    public ICommand ButtonCommand { get { return new RelayCommand(delegate { TriggerPropertyChanges(); }); } }

    public int WindowWidth { get { return 100; } }
    public int WindowHeight { get { return 200; } }
    public int WindowBorderThickness { get { return 8; } }
}

现在,当我点击按钮时,WindowBorderThickness的getter被调用,但不是WindowWidth和WindowHeight的。这一切似乎非常奇怪,与我不一致。我缺少什么?

Now, when I click the button, the getter of WindowBorderThickness is called, but not the ones for WindowWidth and WindowHeight. It all just seems very weird and inconsistent to me. What am I missing?

推荐答案

我会尝试回答我自己的问题。绑定正在工作,但是我们不能确定布局系统要求例如。窗口的Width属性。

I will try to answer my own question. The bindings are working, but we can't really be sure that the layout system asks for e.g. the Width property of the window.

MSDN


如果这个元素是一些其他元素中的子元素,然后将此属性设置为一个值,实际上只是一个建议值。布局系统以及父元素的特定布局逻辑将在布局过程中将该值用作非绑定输入。实际上,一个FrameworkElement几乎总是别的东西的子元素;即使在Window上设置Height。 (对于Window,当底层应用程序模型确定了创建承载应用程序的Hwnd的基本渲染假设时使用该值。)

If this element is a child element within some other element, then setting this property to a value is really only a suggested value. The layout system as well as the particular layout logic of the parent element will use the value as a nonbinding input during the layout process. In practical terms, a FrameworkElement is almost always the child element of something else; even when you set the Height on Window. (For Window, that value is used when the underlying application model establishes the basic rendering assumptions that create the Hwnd that hosts the application.)

似乎有效的解决方案是将WindowWidth属性绑定到MinWidth和MaxWidth以及Width。其中一个将被检索,至少在我上面使用的测试场景中。

A solution that seems to work is to bind the WindowWidth property to MinWidth and MaxWidth, as well as Width. One of these will be retrieved, at least in the test scenario I was using above.

这篇关于绑定到窗口高度和宽度的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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