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)
{
}
}
? – Khan
相対WPF新規ユーザー。私は前に少し束縛してしまったし、決してそれを設定する必要はなかった。私はそれを設定するつもりですか?私はそれを何に設定しますか? –
私はちょうど速いgoogleを持っていて、DataContext = thisを追加しているようです。私のコンストラクタに私の問題を解決します。ありがとうございました! –