简体   繁体   中英

ChildWindow width/height not binding correctly

I am trying to set my child window to the size of my application so it takes up the entire screen. I am using the following code:

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);

Where this is the child window.

I am binding it so that when they resize their browser, the child window should as well. However, my child window isn't binding to the sizes. It still remains its default size. Are my binding incorrect?

I'm not confident you're going to get binding to work. The easiest method to make your ChildWindow fill the screen is just set the HorizontalAlignment & VerticalAlignment to 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">

If you absolutely want to go the ActualWidth/ActualHeight route in silverlight, you'd have to do something like...

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();
}

I think you're trying to bind to ActualWidth.Width , which doesn't exist. Remove the "Width" / "Height" strings from your binding constructor and it should work.

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);

The Content class does not raise a PropertyChanged event when ActualHeight and ActualWidth change; so the Binding has no way of knowing that it needs to refresh the values. There are some complicated ways that you could get around this while still using Binding, but the simplest answer will just be to handle the Content.Resized event and set the values yourself.

If @Rachel's answer doesn't work, you might want to try the technique outlined in this blog posting:

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

According to that post, you cannot bind to readonly properties, which ActualWidth and ActualHeight are.

I don't know if this will work in Silverlight, but it has worked well for us in WPF.

ActualWidth and ActualHeight do not fire PropertyChanged events in Silverlight. This is by design (something about optimizing the performance of the layout engine, if I recall). Thus you should never try to bind to them because it simply won't work. The recommended solution is to handle the SizeChanged event and then update things appropriately yourself. From the documentation :

Do not attempt to use ActualWidth as a binding source for an ElementName binding. If you have a scenario that requires updates based on ActualWidth, use a SizeChanged handler.

Here's a solution that uses attached properties. It should also be straight forward to wrap this functionality in a XAML friendly Blend behavior (probably Behavior<FrameworkElement> ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM