ChildWindow宽度/高度未正确绑定

问题描述:

我试图将我的子窗口设置为我的应用程序的大小,因此占用了整个屏幕。我正在使用以下代码:ChildWindow宽度/高度未正确绑定

Binding widthBinding = new Binding("Width"); 
widthBinding.Source = App.Current.Host.Content.ActualWidth; 
this.SetBinding(ChildWindow.WidthProperty, widthBinding); 

Binding heightBinding = new Binding("Height"); 
heightBinding.Source = App.Current.Host.Content.ActualHeight; 
this.SetBinding(ChildWindow.HeightProperty, heightBinding); 

其中this是子窗口。

我绑定这样,当他们调整自己的浏览器,子窗口也应如此。但是,我的子窗口没有绑定到大小。它仍然保持其默认大小。我的装订有误吗?

我不确定你将会对工作有约束力。最简单的方法,让您的ChildWindow填满屏幕是刚刚设置的Horizo​​ntalAlignment & VerticalAlignment为Stretch

<controls:ChildWindow x:Class="SilverlightApplication4.ChildWindow1" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" 
      Title="ChildWindow1" 
      HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> 

如果你绝对要进去的Silverlight的ActualWidth的/的ActualHeight路线,你必须做的是这样..

public ChildWindow1() 
{ 
    InitializeComponent(); 

    UpdateSize(null, EventArgs.Empty); 

    App.Current.Host.Content.Resized += UpdateSize; 
} 

protected override void OnClosed(EventArgs e) 
{ 
    App.Current.Host.Content.Resized -= UpdateSize; 
} 

private void UpdateSize(object sender, EventArgs e) 
{ 
    this.Width = App.Current.Host.Content.ActualWidth; 
    this.Height = App.Current.Host.Content.ActualHeight; 
    this.UpdateLayout(); 
} 

我想你想绑定到ActualWidth.Width,它不存在。从绑定构造函数中删除"Width"/"Height"字符串,它应该可以工作。

Binding widthBinding = new Binding(); 
widthBinding.Source = App.Current.Host.Content.ActualWidth; 
this.SetBinding(ChildWindow.WidthProperty, widthBinding); 

Binding heightBinding = new Binding(); 
heightBinding.Source = App.Current.Host.Content.ActualHeight; 
this.SetBinding(ChildWindow.HeightProperty, heightBinding); 
+1

这修复了BUG,但因为App.Current.Host.Content不是INotifyPropertyChange接口,它不会动态更新。 – MerickOWA 2012-04-04 16:38:09

+0

@MerickOWA的确如此,我将使用'RelativeSource'绑定来查找父窗口'并以这种方式绑定大小。 – Rachel 2012-04-04 16:40:08

+0

感谢您的建议。我不知道它没有实现INotifyPropertyChange。我用MerickOWA的解决方案,因为它是最简单的。 – Justin 2012-04-04 17:00:59

当ActualHeight和ActualWidth更改时,Content类不会引发PropertyChanged事件;所以绑定无法知道它需要刷新值。有一些复杂的方法可以解决这个问题,同时仍然使用绑定,但最简单的答案只是处理Content.Resized事件并自己设置值。

@如果Rachel的答案是不行的,你可能会想尝试在这个博客中描述的技术:

http://meleak.wordpress.com/2011/08/28/onewaytosource-binding-for-readonly-dependency-property/

根据这个帖子,你不能绑定到只读属性,ActualWidth的和ActualHeight是。

我不知道这是否会在Silverlight的工作,但它一直很适合我们在WPF。

+0

他没有试图绑定到ActualWidth&ActualHeight,他试图绑定从ActualWidth&ActualHeight – MerickOWA 2012-04-04 16:37:13

ActualWidth和ActualHeight不会在Silverlight中触发PropertyChanged事件。这是通过设计(关于优化布局引擎的性能,如果我记得的话)。因此你不应该试图绑定他们,因为它根本无法工作。推荐的解决方案是处理SizeChanged事件,然后自己更新适当的东西。从documentation

不要尝试使用ActualWidth作为 ElementName绑定的绑定源。如果您的场景需要根据ActualWidth更新 ,请使用SizeChanged处理程序。

Here's a solution使用附加属性。也应该直接将这个功能包装在适合XAML的Blend行为中(可能是Behavior<FrameworkElement>)。