绑定到窗口高度和宽度的问题当我尝试将窗口的高度和宽度绑定到视图模型中的属性时,我遇到了一些问题。这是一个小示例应用程序来说明问题。这是app.xaml.xspublicpartialclassApp中的代码:主窗口mainWindow=newMainWindow();MainWindowViewModelmainWindowViewModel=newMainWindowViewModel();主窗口.DataContext=mainWindowViewModel;mainWindow.Show();这是MainWindow.xaml:这是ViewModel:publicclassMainWindowViewModel{publicintWindowWidth{get{return100;}}publicintWindowHeight{get{返回200;}}publicintWindowBorderThickness{get{return8;当程序启动时,WindowHeight和WindowBorderThickness(但不是WindowWidth)的getter被调用,因此窗口的高度和边框设置正确,但宽度设置不正确。然后我添加了将触发所有属性的PropertyChanged的??按钮,以便视图模型现在看起来像这样:publicclassMainWindowViewModel:INotifyPropertyChanged{publicvoidTriggerPropertyChanges(){if(PropertyChanged!=null){PropertyChanged(this,newPropertyChangedEventArgs("WindowWidth"));PropertyChanged(这个,新的PropertyChangedEventArgs(“WindowHeight”));PropertyChanged(这个,新的PropertyChangedEventArgs(“WindowBorderThickness”));}}delpublicICommandButtonCommand{get{reCommand(newRelayTriggerPropertyChanges();});}}publicintWindowWidth{get{返回100;}}publicintWindowHeight{get{返回200;}}publicintWindowBorderThickness{get{return8;现在,当我单击按钮时,将调用WindowBorderThickness的getter,但不会调用WindowWidth和WindowHeight的getter。这一切对我来说似乎很奇怪和不一致。我错过了什么?尝试使用双向绑定,它对我有用:Width="{BindingPath=xExt,Mode=TwoWay}"我遇到了同样的问题,我注意到这取决于首先用xaml编写高度还是宽度。如果高度在前,则Binding仅适用于它,反之亦然。解决方案是将绑定模式设置为“TwoWay”:我所做的项目是使用MSStudio2010和.NET4.0我将尝试回答我自己的问题。绑定工作正常,但我们无法确定布局系统是否需要例如窗口的Width属性。来自MSDN:如果此元素是某个其他元素中的子元素,则将此属性设置为一个值实际上只是一个建议值。布局系统,以及父元素的特定布局逻辑,将在布局期间使用此值作为非绑定输入。事实上,一个FrameworkElement几乎总是其他东西的孩子;即使您在窗口上设置高度。(对于Window,当底层应用程序模型建立创建承载应用程序的Hwnd的底层呈现假设时,将使用此值。)似乎有效的解决方案是将WindowWidth属性绑定到MinWidth和MaxWidth,以及Width。其中之一将被检索,至少在我上面使用的测试场景中是这样。此外,您可以将SizeToContent="WidthAndHeight"与MinHeight和MinWidth一起使用,因此MaxHeight和MaxWidth不需要额外调用。好的,我遇到了同样的问题,无法通过XAML将窗口尺寸(最小、最大、正常)正确绑定到我的视图模型。我不知道为什么,但是如果您通过代码而不是XAML来实现它们,那么您可以毫无问题地实现所有这些绑定。这是我的C#代码:this.SetBinding(Window.WidthProperty,newBinding("Width"){Source=MyViewModel,Mode=BindingMode.TwoWay});this.SetBinding(Window.HeightProperty,newBinding("Height"){Source=MyViewModel,Mode=BindingMode.TwoWay});this.SetBinding(Window.MaxWidthProperty,newBinding("MaxWidth"){Source=MyViewModel});this.SetBinding(Window.MaxHeightProperty,newBinding("MaxHeight"){Source=MyViewModel});this.SetBinding(Window.MinWidthProperty,newBinding("MinWidth"){Source=MyViewModel});this.SetBinding(Window.MinHeightProperty,newBinding("MinHeight"){Source=MyViewModel});奇怪的是,它只适用于代码,不适用于XAML。更奇怪的是,默认情况下它为mMin和Max维度绑定TwoWay,但它不绑定必须指定?Mode=BindingMode.TwoWay?的Normal维度。应该有一个微软必须纠正的错误......MinWidth和MinHeight的绑定是正确的。另外,如果你需要绑定到MaxWidth和MaxHeight,如果你的动态会放大或缩小Window的大小。我不确定您的确切实施方式,但我只是在编写一个可能有效的示例。XAML代码MainWindow公共共享只读WindowWidthProperty作为DependencyProperty=_DependencyProperty.Register("WindowWidth",_GetType(Integer),GetType(MainWindow),_NewFrameworkPropertyMetadata(Nothing))PublicSharedReadOnlyWindowHeightPropertyAsDependencyProperty=_DependencyProperty.Register("WindowHeight",_GetType(Integer),GetType(MainWindow),_NewFrameworkPropertyMetadata(Nothing))PublicPropertyWindowWidthAsIntegerGetReturnCInt(GetValue(WindowWidthProperty))EndGetSet(ByValvalueAsInteger)SetValue(WindowWidthProperty,值)EndSetEndPropertyPublicPropertyWindowHeightAsIntegerGetReturnCInt(GetValue(WindowHeightProperty))EndGetSet(ByValvalueAsInteger)SetValue(WindowHeightProperty,value)EndSetEndPropertyEndClassC#代码以上是C#学习教程:binding如果你想了解更多C#学习教程,如果对你有用,需要了解更多C#学习教程,希望你多多关注——publicreadonlyDependencyPropertyWindowWidthProperty=DependencyProperty。Register("WindowWidth",typeof(Double),typeof(MainWindow),newFrameworkPropertyMetadata());publicreadonlyDependencyPropertyWindowHeightProperty=DependencyProperty。Register("WindowHeight",typeof(Double),typeof(MainWindow),newFrameworkProperty))tyMetadata(;publicdoubleWindowWidth{get{returnConvert.ToDouble(this.GetValue(WindowWidthProperty));}set{this.SetValue(WindowWidthProperty,value);}}publicdoubleWindowHeight{get{returnConvert.ToDouble(this.GetValue(WindowHeightProperty));}set{this.SetValue(WindowHeightProperty,value);}}本文收集自网络,不代表一个职位,如涉及侵权,请点击右侧联系管理员删除。
