This is probably a basic question, since I'm new to WPF..
I have a UserControl that contains a TextBox and a Button (code is simplified for this question) :
<UserControl x:Name="this">
<TextBox Text="{Binding ElementName=this, Path=MyProperty.Value}"/>
<Button x:Name="MyButton" Click="Button_Click"/>
</UserControl>
In the code behind I have registered "MyProperty" as DependencyProperty:
public static readonly DependencyProperty MyPropertyProperty = DependencyProperty.Register("MyProperty", typeof(MyProperty), typeof(MyPropertyNumeric), new UIPropertyMetadata(null));
A "MyProperty" is a class defined by me, that implements INotifyPropertyChanged. "MyProperty.Value" is of type object.
When the button is clicked, I change MyProperty.Value in the code-behind. I want to have the TextBox to automatically show the new value. I would expect that the above would work, since I've implemented INotifyPropertyChanged - but it doesn't.. Anyone knows how to do this?
Are you calling the OnPropertyChanged event with the name of your property when it is updated?
Eg,
public class MyProperty : INotifyPropertyChanged {
private string _value;
public string Value { get { return _value; } set { _value = value; OnPropertyChanged("Value"); } }
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name) {
PropertyChangedEventHandler handler = PropertyChanged;
if(handler != null) {
handler(this, new PropertyChangedEventArgs(name));
}
}
}
It is important to make sure the PropertyChanged event is fired with the name of the property you want to update.
尝试添加到Binding Mode =“ OneWay”,我不确定到底该怎么做
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.