2012-03-07 11 views
4

Windowから派生したクラス(MainWindow)のプロパティ(MyTitle)の値をバインドしようとしています。 MyTitlePropertyという依存関係プロパティを作成し、INotifyPropertyChangedインターフェイスを実装し、プロパティ名パラメータとして「MyTitle」を渡してPropertyChangedイベントを呼び出すようMyTitleのsetメソッドを変更しました。 MyTitleをコンストラクタ内の "Title"に設定しますが、ウィンドウが開いたときにタイトルが空白になります。 Loadedイベントにブレークポイントを設定すると、MyTitle = "Title" but this.Title = ""となります。これは私が気付かなかったことは確かに信じられないほど明白です。助けてください!WPFウィンドウのプロパティへのバインドタイトル

MainWindow.xaml

<Window 
    x:Class="WindowTitleBindingTest.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:this="clr-namespace:WindowTitleBindingTest" 
    Height="350" 
    Width="525" 
    Title="{Binding Path=MyTitle, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type this:MainWindow}}}" 
    Loaded="Window_Loaded"> 
    <Grid> 

    </Grid> 
</Window> 

MainWindow.xaml.cs:

public partial class MainWindow : Window, INotifyPropertyChanged 
{ 
    public static readonly DependencyProperty MyTitleProperty = DependencyProperty.Register("MyTitle", typeof(String), typeof(MainWindow)); 

    public String MyTitle 
    { 
     get { return (String)GetValue(MainWindow.MyTitleProperty); } 
     set 
     { 
      SetValue(MainWindow.MyTitleProperty, value); 
      OnPropertyChanged("MyTitle"); 
     } 
    } 

    public MainWindow() 
    { 
     InitializeComponent(); 

     MyTitle = "Title"; 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    protected void OnPropertyChanged(String propertyName) 
    { 
     if (PropertyChanged != null) 
      PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 
    } 
} 
+3

? – Khan

+0

相対WPF新規ユーザー。私は前に少し束縛してしまったし、決してそれを設定する必要はなかった。私はそれを設定するつもりですか?私はそれを何に設定しますか? –

+1

私はちょうど速いgoogleを持っていて、DataContext = thisを追加しているようです。私のコンストラクタに私の問題を解決します。ありがとうございました! –

答えて

20
public MainWindow() 
{ 
    InitializeComponent(); 

    DataContext = this; 

    MyTitle = "Title"; 
} 

次に、あなただけの、あなたが必要としないXAMLで

Title="{Binding MyTitle}" 

が必要依存関係プロパティ

3
Title="{Binding Path=MyTitle, RelativeSource={RelativeSource Mode=Self}}" 
6

まず第一に、あなただけのDependencyPropertyにバインドしたい場合は、INotifyPropertyChangedは必要ありません。それは冗長です。

DataContextのいずれかを設定する必要はありません。これは、ViewModelシナリオの場合です。 (チャンスを得るたびにMVVMパターンを調べてください)。依存関係プロパティのあなたの宣言が間違っている

さて、それは次のようになります。

public string MyTitle 
     { 
      get { return (string)GetValue(MyTitleProperty); } 
      set { SetValue(MyTitleProperty, value); } 
     } 

     // Using a DependencyProperty as the backing store for MyTitle. This enables animation, styling, binding, etc... 
     public static readonly DependencyProperty MyTitleProperty = 
      DependencyProperty.Register("MyTitle", typeof(string), typeof(MainWindow), new UIPropertyMetadata(null)); 

UIPropertyMetadataに注意してください:それはあなたのDPのデフォルト値を設定します。あなたのXAMLで

そして最後に、:あなたのDataContextが設定されている

<Window ... 
     Title="{Binding MyTitle, RelativeSource={RelativeSource Mode=Self}}" 
     ... /> 
関連する問題