Simple problem with OneWay Binding in WPF
up vote
1
down vote
favorite
I have simple class with one property, this class implements the interface INotifyPropertyChange.
public class SomeClass : INotifyPropertyChanged
{
private string _iD;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string ID
{
get { return _iD; }
set
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("ID can not be null or empty");
if (this.ID != value)
{
_iD = value;
NotifyPropertyChanged(ID);
}
}
}
}
I am trying make a OneWay binding to a label. I set the label's DataContext in the code behind.
private SomeClass _myObject;
public MainWindow()
{
InitializeComponent();
_myObject = new SomeClass() { ID = "SomeID" };
lb.DataContext = _myObject;
}
In the XAML I am binding the property ID to the Content of the label.
<Label Name="lb" Content="{Binding Path = ID, Mode=OneWay}" Grid.Row="0"></Label>
<TextBox Name="tb" Grid.Row="1"></TextBox>
<Button Name="btn" Content="Change" Height="20" Width="100" Grid.Row="2" Click="btn_Click"></Button>
Then I change value of property ID in the button click event, but the content of label isn't changing.
private void btn_Click(object sender, RoutedEventArgs e)
{
_myObject.ID = tb.Text;
Title = _myObject.ID;
}
Why is this not working?
c# wpf binding
add a comment |
up vote
1
down vote
favorite
I have simple class with one property, this class implements the interface INotifyPropertyChange.
public class SomeClass : INotifyPropertyChanged
{
private string _iD;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string ID
{
get { return _iD; }
set
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("ID can not be null or empty");
if (this.ID != value)
{
_iD = value;
NotifyPropertyChanged(ID);
}
}
}
}
I am trying make a OneWay binding to a label. I set the label's DataContext in the code behind.
private SomeClass _myObject;
public MainWindow()
{
InitializeComponent();
_myObject = new SomeClass() { ID = "SomeID" };
lb.DataContext = _myObject;
}
In the XAML I am binding the property ID to the Content of the label.
<Label Name="lb" Content="{Binding Path = ID, Mode=OneWay}" Grid.Row="0"></Label>
<TextBox Name="tb" Grid.Row="1"></TextBox>
<Button Name="btn" Content="Change" Height="20" Width="100" Grid.Row="2" Click="btn_Click"></Button>
Then I change value of property ID in the button click event, but the content of label isn't changing.
private void btn_Click(object sender, RoutedEventArgs e)
{
_myObject.ID = tb.Text;
Title = _myObject.ID;
}
Why is this not working?
c# wpf binding
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have simple class with one property, this class implements the interface INotifyPropertyChange.
public class SomeClass : INotifyPropertyChanged
{
private string _iD;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string ID
{
get { return _iD; }
set
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("ID can not be null or empty");
if (this.ID != value)
{
_iD = value;
NotifyPropertyChanged(ID);
}
}
}
}
I am trying make a OneWay binding to a label. I set the label's DataContext in the code behind.
private SomeClass _myObject;
public MainWindow()
{
InitializeComponent();
_myObject = new SomeClass() { ID = "SomeID" };
lb.DataContext = _myObject;
}
In the XAML I am binding the property ID to the Content of the label.
<Label Name="lb" Content="{Binding Path = ID, Mode=OneWay}" Grid.Row="0"></Label>
<TextBox Name="tb" Grid.Row="1"></TextBox>
<Button Name="btn" Content="Change" Height="20" Width="100" Grid.Row="2" Click="btn_Click"></Button>
Then I change value of property ID in the button click event, but the content of label isn't changing.
private void btn_Click(object sender, RoutedEventArgs e)
{
_myObject.ID = tb.Text;
Title = _myObject.ID;
}
Why is this not working?
c# wpf binding
I have simple class with one property, this class implements the interface INotifyPropertyChange.
public class SomeClass : INotifyPropertyChanged
{
private string _iD;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string ID
{
get { return _iD; }
set
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("ID can not be null or empty");
if (this.ID != value)
{
_iD = value;
NotifyPropertyChanged(ID);
}
}
}
}
I am trying make a OneWay binding to a label. I set the label's DataContext in the code behind.
private SomeClass _myObject;
public MainWindow()
{
InitializeComponent();
_myObject = new SomeClass() { ID = "SomeID" };
lb.DataContext = _myObject;
}
In the XAML I am binding the property ID to the Content of the label.
<Label Name="lb" Content="{Binding Path = ID, Mode=OneWay}" Grid.Row="0"></Label>
<TextBox Name="tb" Grid.Row="1"></TextBox>
<Button Name="btn" Content="Change" Height="20" Width="100" Grid.Row="2" Click="btn_Click"></Button>
Then I change value of property ID in the button click event, but the content of label isn't changing.
private void btn_Click(object sender, RoutedEventArgs e)
{
_myObject.ID = tb.Text;
Title = _myObject.ID;
}
Why is this not working?
c# wpf binding
c# wpf binding
edited Nov 19 at 16:17
Max Young
7541630
7541630
asked Oct 31 '10 at 10:58
Tom
64311
64311
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
NotifyPropertyChanged should take the name of the changed property and not the value of the property. So change NotifyPropertyChanged(ID) to NotifyPropertyChanged("ID").
Thank you for your advance
– Tom
Oct 31 '10 at 12:26
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
NotifyPropertyChanged should take the name of the changed property and not the value of the property. So change NotifyPropertyChanged(ID) to NotifyPropertyChanged("ID").
Thank you for your advance
– Tom
Oct 31 '10 at 12:26
add a comment |
up vote
3
down vote
NotifyPropertyChanged should take the name of the changed property and not the value of the property. So change NotifyPropertyChanged(ID) to NotifyPropertyChanged("ID").
Thank you for your advance
– Tom
Oct 31 '10 at 12:26
add a comment |
up vote
3
down vote
up vote
3
down vote
NotifyPropertyChanged should take the name of the changed property and not the value of the property. So change NotifyPropertyChanged(ID) to NotifyPropertyChanged("ID").
NotifyPropertyChanged should take the name of the changed property and not the value of the property. So change NotifyPropertyChanged(ID) to NotifyPropertyChanged("ID").
answered Oct 31 '10 at 11:04
Jakob Christensen
12.8k14075
12.8k14075
Thank you for your advance
– Tom
Oct 31 '10 at 12:26
add a comment |
Thank you for your advance
– Tom
Oct 31 '10 at 12:26
Thank you for your advance
– Tom
Oct 31 '10 at 12:26
Thank you for your advance
– Tom
Oct 31 '10 at 12:26
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f4062829%2fsimple-problem-with-oneway-binding-in-wpf%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown