は私の個人クラスである:ここではカスタムクラスに依存オブジェクト/プロパティを実装する方法は?ここ
public class Person
{
private string _lastName;
public string LastName
{
get { return _lastName; }
set { _lastName = value; }
}
}//close class
は私のXAMLです:それを "btnChangeValue" をクリックした後:ここで
<StackPanel>
<TextBox x:Name="txtLastName"
Height="50" Width="300"
DataContext="{Binding ElementName=_this, Path=PersonObject}"
Text="{Binding Path=LastName}" />
<Button Height="50" Width="100" x:Name="btnChangeValue" Content="Change Value" Click="btnChangeValue_Click"/>
</StackPanel>
は、私の質問があり、私のXAML.CS
public partial class ClassDependency : Window
{
public Person objPerson = new Person();
public ClassDependency()
{
objPerson.LastName = "testing...";
InitializeComponent();
}
public Person PersonObject
{
get { return objPerson; }
set { objPerson = value; }
}
private void btnChangeValue_Click(object sender, RoutedEventArgs e)
{
objPerson.LastName = "New value after click....";
}
}//close class
です私のコードで姓を変更していますが、それは私のテキストボックス "txtLastName"の反映ではありません。どうすればこの問題を解決できますか?私はxaml.csファイルにDependency Propertyを実装する必要がありますか?私はそれも試みたが使用しなかった。
public static readonly DependencyProperty PersonObjectProperty = DependencyProperty.Register("PersonObject", typeof(object), typeof(ClassDependency));
public Person PersonObject
{
get { return (Person)GetValue(PersonObjectProperty); }
set { SetValue(PersonObjectProperty, value); }
}
私は何をすべきですか?アドバイスしてください。..
さらに詳しい説明:私のTextBoxバインディングに_thisが表示されます。 _これは私の現在のウィンドウ名です。私が窓を走らせるとき。テキストボックスに「テスト」が表示されますが、ボタンをクリックしても値は変わりません。 –
usergaro