2012-01-13 6 views
2

私はコードビハインドクラスからバインドしようとしている幅と高さのMainViewウィンドウを持っています。MainViewウィンドウのWidthプロパティへのバインド時のバインディングエラー

<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 'でバインド元が見つかりません。 BindingExpression:Path = WindowHeight; DataItem = null;ターゲット要素は 'MainView'(Name = '')です。ターゲットのプロパティは 'Height'(タイプ 'Double')です。

と同様のものがWindowWidthです。

私は結合様式を理解すると思ったが、私は私が間違っていたと推測:(

私は窓を探してFindAncestorを使用すると、それはそのMAINVIEWウィンドウを見つけるまで、それはビジュアルツリーを見上げるさせるようなことを考えていましたその上で、私の特性を有していた。

すべてのヘルプははるかに高く評価されるだろう。

答えて

2

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

分離コードが同じelemはあり試しますあなたの窓。自己バインディングを使用してください。

+0

ありがとうございました! – Pat

関連する問題