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

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

问题描述

我有一些问题,当我尝试的高度和宽度的一个窗口,以属性绑定在我看来模型。这里是一个小示例应用程序来说明这个问题。这是code在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>

这是视图模型:

And this is the view model:

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

当开始该程序WINDOWHEIGHT和WindowBorderThickness(但不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的所有属性,使视图模型现在看起来是这样的:

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的吸气剂被调用,而不是那些为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.

从<一个href="http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.height%28v=VS.100%29.aspx">MSDN:

如果这个元素内的一些其他元素的子元素,然后设置该属性的值是真的只是一个建议值。布局系统,以及父元素的特殊布局逻辑将使用该值作为在布局过程中一个不具约束力的输入。在实际应用中,一个FrameworkElement的几乎总是的别的子元件;即使你设置窗口的高度。 (对于Windows,则使用该值时,底层的应用模型规定,在承载应用程序的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,以及宽绑定。其中的一个将被检索的,至少在该测试方案,我使用上文

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天全站免登陆