【问题标题】:ChildWindow width/height not binding correctlyChildWindow 宽度/高度未正确绑定
【发布时间】:2012-04-04 16:14:38
【问题描述】:

我正在尝试将我的子窗口设置为我的应用程序的大小,以便它占据整个屏幕。我正在使用以下代码:

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 是子窗口。

我正在绑定它,以便当他们调整浏览器大小时,子窗口也应该如此。但是,我的子窗口没有绑定到大小。它仍然保持其默认大小。我的绑定不正确吗?

【问题讨论】:

    标签: c# .net wpf silverlight


    【解决方案1】:

    我不相信你会得到约束工作。让 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();
    }
    

    【讨论】:

      【解决方案2】:

      我认为您正在尝试绑定到不存在的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);
      

      【讨论】:

      • 这修复了他的错误,但由于 App.Current.Host.Content 不是 INotifyPropertyChange 接口,它不会动态更新。
      • @MerickOWA 没错,我会使用RelativeSource 绑定来找到父Window 并以这种方式绑定大小。
      • 感谢您的建议。我不知道它没有实现 INotifyPropertyChange。我选择了 MerickOWA 的解决方案,因为它是最简单的。
      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        如果@Rachel 的回答不起作用,您可能想尝试这篇博文中概述的技术:

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

        根据该帖子,您不能绑定到只读属性,即 ActualWidth 和 ActualHeight。

        我不知道这是否适用于 Silverlight,但它在 WPF 中对我们来说效果很好。

        【讨论】:

        • 他不是在尝试绑定到 ActualWidth 和 ActualHeight,他是在尝试绑定 FROM ActualWidth 和 ActualHeight
        【解决方案5】:

        ActualWidth 和 ActualHeight 不会在 Silverlight 中触发 PropertyChanged 事件。这是设计使然(关于优化布局引擎的性能,如果我记得的话)。因此,您永远不应该尝试绑定它们,因为它根本行不通。推荐的解决方案是处理 SizeChanged 事件,然后自己进行适当的更新。来自documentation

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

        Here's a solution 使用附加属性。还应该直接将此功能包装在 XAML 友好的 Blend 行为中(可能是 Behavior&lt;FrameworkElement&gt;)。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-05-23
          • 1970-01-01
          • 1970-01-01
          • 2020-01-31
          • 1970-01-01
          • 2017-05-16
          • 1970-01-01
          相关资源
          最近更新 更多