绑定到窗口高度和宽度的问题

当我尝试将窗口的高度和宽度绑定到视图模型中的属性时,我遇到了一些问题。 这是一个小样本应用程序来说明问题。 这是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:

   

这是视图模型:

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

程序启动时,会调用WindowHeight和WindowBorderThickness(但不是WindowWidth)的getter,因此窗口的高度和边框设置正确,但不是宽度。

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

 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的getter。 这一切看起来都非常奇怪而且与我不一致。 我错过了什么?

尝试使用双向绑定,它对我有用:

 Width="{Binding Path=xExt, Mode=TwoWay}" 

我有同样的问题,我注意到它取决于是否首先在xaml中写入高度或宽度。 如果高度是第一个,那么Binding仅适用于它,反之亦然。 解决方案是将Binding模式设置为’TwoWay’:我所做的项目是使用MS Studio 2010和.NET 4.0

我会尽力回答我自己的问题。 绑定工作正常,但我们无法确定布局系统是否要求例如窗口的Width属性。

来自MSDN :

如果此元素是某个其他元素中的子元素,则将此属性设置为值实际上只是建议值。 布局系统以及父元素的特定布局逻辑将在布局过程中将该值用作非绑定输入。 实际上,FrameworkElement几乎总是其他东西的子元素; 即使你在窗口设置高度。 (对于Window,当底层应用程序模型建立创建承载应用程序的Hwnd的基本呈现假设时,将使用该值。)

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

此外,您可以将SizeToContent="WidthAndHeight"MinHeightMinWidth ,因此MaxHeightMaxWidth不需要额外调用。

好,

我有同样的问题,无法通过XAML正确绑定窗口尺寸(最小值,最大值,正常值)到我的viewmodel。

我不知道为什么,但如果你通过代码而不是XAML来实现它们,那么你可以毫无问题地实现所有这些绑定。

这是我的C#代码有效:

 this.SetBinding(Window.WidthProperty, new Binding("Width") { Source = MyViewModel, Mode=BindingMode.TwoWay }); this.SetBinding(Window.HeightProperty, new Binding("Height") { Source = MyViewModel, Mode=BindingMode.TwoWay }); this.SetBinding(Window.MaxWidthProperty, new Binding("MaxWidth") { Source = MyViewModel }); this.SetBinding(Window.MaxHeightProperty, new Binding("MaxHeight") { Source = MyViewModel }); this.SetBinding(Window.MinWidthProperty, new Binding("MinWidth") { Source = MyViewModel }); this.SetBinding(Window.MinHeightProperty, new Binding("MinHeight") { Source = MyViewModel }); 

奇怪的是它只能在代码中工作,而不能在XAML中工作。 更为奇怪的是,默认情况下,对于mMin和Max尺寸,它会绑定TwoWay,但是对于必须指定«Mode = BindingMode.TwoWay»的Normal尺寸,它不会。

应该有一个微软必须纠正的错误…

绑定到MinWidth和MinHeight是正确的。 此外,如果您需要绑定到MaxWidth和MaxHeight,如果您的动态将放大或缩小Window的大小。

我不确定你的具体实现,但我只是写一个可能有用的例子。

XAML

         

 Class MainWindow Public Shared ReadOnly WindowWidthProperty As DependencyProperty = _ DependencyProperty.Register("WindowWidth", _ GetType(Integer), GetType(MainWindow), _ New FrameworkPropertyMetadata(Nothing)) Public Shared ReadOnly WindowHeightProperty As DependencyProperty = _ DependencyProperty.Register("WindowHeight", _ GetType(Integer), GetType(MainWindow), _ New FrameworkPropertyMetadata(Nothing)) Public Property WindowWidth As Integer Get Return CInt(GetValue(WindowWidthProperty)) End Get Set(ByVal value As Integer) SetValue(WindowWidthProperty, value) End Set End Property Public Property WindowHeight As Integer Get Return CInt(GetValue(WindowHeightProperty)) End Get Set(ByVal value As Integer) SetValue(WindowHeightProperty, value) End Set End Property End Class 

C#代码

 public readonly DependencyProperty WindowWidthProperty = DependencyProperty.Register("WindowWidth", typeof(Double), typeof(MainWindow), new FrameworkPropertyMetadata()); public readonly DependencyProperty WindowHeightProperty = DependencyProperty.Register("WindowHeight", typeof(Double), typeof(MainWindow), new FrameworkPropertyMetadata()); public double WindowWidth { get { return Convert.ToDouble(this.GetValue(WindowWidthProperty)); } set { this.SetValue(WindowWidthProperty, value); } } public double WindowHeight { get { return Convert.ToDouble(this.GetValue(WindowHeightProperty)); } set { this.SetValue(WindowHeightProperty, value); } }