【问题标题】:Binding error when binding to Width Property of MainView Window绑定到 MainView 窗口的 Width 属性时出现绑定错误
【发布时间】:2012-02-09 10:01:10
【问题描述】:

我有一个 MainView 窗口,我试图从它的 Code-Behind 类绑定其宽度和高度。

<Window x:Class="RpP25.MainView"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   xmlns:shared="http://schemas.actiprosoftware.com/winfx/xaml/shared"     
   xmlns:docking="http://schemas.actiprosoftware.com/winfx/xaml/docking"
   xmlns:themes="http://schemas.actiprosoftware.com/winfx/xaml/themes"
   xmlns:RpWin="clr-namespace:RpP25"
   xmlns:RpWinCmds="clr-namespace:RpP25.Commands"
   xmlns:diagnostics="clr-namespace:System.Diagnostics;assembly=WindowsBase"
   Title="{Binding Path=FileName, Converter={StaticResource WindowTitleNameConverter}}"
   Height="{Binding Path=WindowHeight, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
   Width="{Binding Path=WindowWidth, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"
   Icon="images/CobWhite.ico"
   Loaded="Window_Loaded"
   Closing="Window_Closing">

后面的代码具有属性,并且还通过 ViewModel MainVM 将数据上下文设置为。

public partial class MainView : Window, INotifyPropertyChanged
{
    // Constant Fields
    private const double windowDefaultWidth_ = 720;
    private const double windowDefaultHeight_ = 400;

    // Private Fields
    private RegistryKey RegKey_;                                // Registry Key to hold Registry Subkey
    private WindowState windowState_ = WindowState.Normal;   // Display State of the MainWindow (Min,Max or Normal)
    private double windowWidth_ = windowDefaultWidth_;    // Width of the MainWindow
    private double windowHeight_ = windowDefaultHeight_;  // Height of the MainWindow

    #region Constructors

    public MainView()
    {
        InitializeComponent();
        DataContext = new MainVM();

        // Get the state of the window and the width/height from the registry
        ReadRegistryValues();

    /// <summary>
    /// Gets the Width of the Window
    /// </summary>
    public double WindowWidth
    {
        get { return windowWidth_; }
        set { windowWidth_ = value; NotifyPropertyChanged("WindowWidth"); }
    }

    /// <summary>
    /// Gets the Height of the Window
    /// </summary>
    public double WindowHeight
    {
        get { return windowHeight_; }
        set { windowHeight_ = value; NotifyPropertyChanged("windowHeight"); }
    }

    ...
}

这个窗口是从App::OnStartup()创建的。

当我运行代码时,调试器在输出窗口中抛出以下错误。

找不到与引用“RelativeSource FindAncestor, AncestorType='System.Windows.Window', AncestorLevel='1'' 进行绑定的源。绑定表达式:路径=窗口高度;数据项=空;目标元素是'MainView'(名称='');目标属性是“高度”(类型“双”)

还有一个类似的WindowWidth

我以为我理解绑定模式,但我想我错了:(

我在想,使用 FindAncestor 查找窗口会导致它查找可视化树,直到找到包含我的属性的 MainView 窗口。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# wpf data-binding


    【解决方案1】:

    试试

    Width="{Binding Path=WindowWidth, RelativeSource={RelativeSource Self}}"
    

    Codebehind 是与您的窗口相同的元素。所以使用自绑定。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      相关资源
      最近更新 更多