Xamarin.Forms simple binding to Label TextProperty - c#

I am new to Xamarin.Forms and the binding concept. Can someone please tell me why this is not working? The name of the object itself is changing when I'm pressing the button. Why wont the Text-property update?
var red = new Label
{
Text = todoItem.Name,
BackgroundColor = Color.Red,
Font = Font.SystemFontOfSize (20)
};
red.SetBinding (Label.TextProperty, "Name");
Button button = new Button
{
Text = String.Format("Tap for name change!")
};
button.Clicked += (sender, args) =>
{
_todoItem.Name = "Namie " + new Random().NextDouble();
};
The todoItem is an object of the class below. The notification itself works, I am almost positive. I guess there's something wrong with my binding, or I am missing something with this concept.
public class TodoItem : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
string _name;
public string Name
{
get { return _name; }
set
{
if (value.Equals(_name, StringComparison.Ordinal))
{
// Nothing to do - the value hasn't changed;
return;
}
_name = value;
OnPropertyChanged();
}
}
void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}

You need to set the Label's BindingContext:
red.BindingContext = _todoItem;

Related

c# wpf data binding not happening.

I am a bit new on c# WPF.
I have been following MVVM pattern and everything is set, my code seem to work fine but Issue I am facing is when I bind the data on xaml file, the data I am receiving from get set property but binding seems to have gone as no data is displayed on my text box. check my code.
/**********************xaml code***********************************\
<UserControl x:Class="ILS.debugger.debuggermsg"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:serial="clr-namespace:ILS.VM.Serial_Monitor;assembly=ILS.VM"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<TextBox Text="{Binding Debugger_Recoreded}" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" Background="#FFEBD3D3">
</TextBox>
</Grid>
</UserControl>
/***********************viewmodel code******************\
namespace ILS.VM.Serial_Monitor
{
public class serial : NotifyPropertyChanged
{
private string debuger_rec;
public string Debugger_Recoreded
{
get { return debuger_rec; }
set
{
if (this.debuger_rec == value)
return;
this.debuger_rec = value;
i--;
if (i == 0)
{
this.debuger_rec = String.Empty;
i = 1000;
}
this.InvokePropertyChanged("Debugger_Recoreded");
}
}
/***********************model******************\
namespace ILS
public void OnDebugger(String Receved_Data) //debug message monitor code
{
try
{
this.serialData.Debugger_Recoreded += " " + DateTime.Now + " " + Receved_Data + Environment.NewLine;
this.serialData.Debugger_Recoreded += Environment.NewLine;
}
catch (Exception e)
{
}
}
public class serial : INotifyPropertyChanged
{
private string debuger_rec;
public string Debugger_Recoreded
{
get { return debuger_rec; }
set
{
if (this.debuger_rec == value)
return;
this.debuger_rec = value;
i--;
if (i == 0)
{
this.debuger_rec = String.Empty;
i = 1000;
}
OnPropertyChanged("Debugger_Recoreded");
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
And set DataContext too, in main windows enter this lines:
this.DataContext = serialData;
Also you can use mode way to bind.
<TextBox Text="{Binding Debugger_Recoreded,Mode="Towway"}" />
In your code-behind (i.e your debuggermsg class), you have to instantiate and assign a DataContext:
public debuggermsg()
{
InitializeComponent();
this.DataContext = new serial();
}
It is required for DataBinding, so that you will be able to interact with your ViewModel's properties.
Then, modify your ViewModel like so:
public class serial : INotifyPropertyChanged
{
private string debuger_rec;
public string Debugger_Recoreded
{
get { return debuger_rec; }
set
{
if (this.debuger_rec == value)
return;
this.debuger_rec = value;
i--;
if (i == 0)
{
this.debuger_rec = String.Empty;
i = 1000;
}
OnPropertyChanged("Debugger_Recoreded");
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
Implementation of OnPropertyChanged method is required to notify your view of a modification of your ViewModel's property.
Everything should be fine then.
When implementing INotifyPropertyChanged it's best to use [CallerMemberName] attribute, it's in the System.Runtime.CompilerServices, as you don't have to hardcode a string name of the calling property:
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName]string propertyName = "")
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
Now you can write your property like this:
private string debuggerRecorded;
public string DebuggerRecorded
{
get
{
return debuggerRecorded;
}
set
{
if (debuggerRecorded != value)
{
this.debuggerRecorded = value;
i--;
if (i == 0)
{
this.debuggerRecorded = String.Empty;
i = 1000;
}
OnPropertyChanged(); // No argument needed
}
}
}
By doing this you don't have to worry about spelling and you can freely change the names of your properties in the future, you don't have to remember to change it also in the OnPropertyChanged.
Assuming everything else works fine with your code you just need to set DataContext, which is usually done in MainWindow. For example, like this:
public partial class MainWindow : Window
{
private Serial viewModel;
public MainWindow()
{
InitializeComponent();
viewModel = new Serial();
this.DataContext = viewModel;
}
}
Also, you may want to write your text box with another property:
TextBox Text="{Binding DebuggerRecorded, UpdateSourceTrigger=PropertyChanged}" ...
If you omit this last part, the Text will get updated only when the TextBox loses focus.

label databinding not updating

I am DataBinding to a readonly property to the Text property of a Label in Windows Forms.
lblSourceLoggingState.DataBindings.Add("Text", machine, "Status");
My problem with this is the text only gets updated at startup of the form.
I am implementing INotifyPropertyChanged in a base class like this:
protected void SetValue<T>(Expression<Func<T>> property, T value)
{
LambdaExpression lambdaExpression = property;
if (lambdaExpression == null)
{
throw new ArgumentException("Lambda expression return value can't be null", "property");
}
string propertyName = PropertyName.GetMemberName(lambdaExpression);
T storedValue = getValue<T>(propertyName);
if (Equals(storedValue, value))
return;
_propertyValueStorage[propertyName] = value;
OnPropertyChanged(propertyName);
}
public SynchronizationContext SyncContext;
protected ViewModelBase()
{
SyncContext = SynchronizationContext.Current;
_propertyValueStorage = new Dictionary<string, object>();
}
protected void OnPropertyChanged(string propertyName)
{
var handler = PropertyChanged;
if (handler != null)
{
if (SyncContext != null)
SyncContext.Send(obj => handler(this, new PropertyChangedEventArgs(propertyName));
else
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
And here the Code in the machine object for Status Property:
public short Status
{
get { return GetValue(() => Status); }
private set { SetValue(() => Status, value); }
}
While debugging and stepping through the SetValue or OnPropertyChanged methods the Label text gets updated, but if i run the application without breakpoints it wont.
Also if i register to the PropertyChangedEvent like this:
machine.PropertyChanged += delegate(object sender, PropertyChangedEventArgs args)
{
if (args.PropertyName == "Status")
lblSourceLoggingState.Text = machine.Status.ToString();
};
the label text also gets updated like it should so what could go wrong with the DataBindings?

Xamarin.Forms DataBinding in code for Switch Cell

I have the following object:
public class Notification : INotifyPropertyChanged
{
private bool _trafficNot;
public bool TrafficNot
{
get { return _trafficNot; }
set {
if (value.Equals(_trafficNot))
return;
_trafficNot = value;
OnPropertyChanged();
}
}
private bool _newsNot;
public bool NewsNot
{
get { return _newsNot; }
set
{
if (value.Equals(_newsNot))
return;
_newsNot = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName]String propertyName=null)
{
var handler=PropertyChanged;
if(handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
I get the data from an a object like this:
//set up the notification object according to what is stored in the DB
Notification notification = new Notification
{
TrafficNot = uInfo.NotificationTraffic,
NewsNot = uInfo.NotificationNews
};
and I want to bind the data to these switchells
TableView tableView = new TableView
{
BindingContext = notification,
Intent = TableIntent.Form,
Root = new TableRoot
{
new TableSection
{
new SwitchCell
{
Text = "News",
BindingContext = "NewsNot"
},
new SwitchCell
{
Text = "Traffic",
BindingContext = "TrafficNot"
},
new SwitchCell
}
}
};
What else do i need to do to bind it?
Cheers
You didn't bind view properties at all. Instead of assigning text to BindingContxt and Text property you should bind those Text properties, i.e.:
var sc = new SwitchCell();
sc.SetBinding(SwitchCell.TextProperty, new Binding("NewsNot"));
BindingContext is the source object while you are binding against its properties. See also DataBinding docs.

Forced refresh of a binding Label (C# Windows Form)

I would like to display the name of the object currently built in a label (Windows Form)
My label doesn't refresh in real-time. When I put a DialogBox or something else after each iteration, the label takes the good value and is well updated, but when I let my code run without "breaks", the label seems to bug and his value never changes...
I have the same kind of code for a progress bar and everything is working well.
ModelBuilder.cs
public event PropertyChangedEventHandler PropertyChanged;
private Substation currentSubstation;
public Substation CurrentSubstation
{
get
{
return this.currentSubstation;
}
set
{
if (value != this.currentSubstation)
{
this.currentSubstation = value;
NotifyPropertyChanged();
}
}
}
private void NotifyPropertyChanged(String propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public void Build()
{
foreach (string substationUri in substationsUri)
{
Substation substation = new Substation(new Uri(substationUri));
this.CurrentSubstation = substation;
/* code */
}
}
View.cs
private void StartImportation_Click(object sender, EventArgs e)
{
this.model = new ModelBuilder(....);
this.myLabel.DataBindings.Add(new Binding("Text", this.model, "CurrentSubstation.name"));
this.model.Build();
}

Binding property to control in Winforms

What is the best way to bind a property to a control so that when the property value is changed, the control's bound property changes with it.
So if I have a property FirstName which I want to bind to a textbox's txtFirstName text value. So if I change FirstName to value "Stack" then the property txtFirstName.Text also changes to value "Stack".
I know this may sound a stupid question but I'll appreciate the help.
You must implement INotifyPropertyChanged And add binding to textbox.
I will provide C# code snippet. Hope it helps
class Sample : INotifyPropertyChanged
{
private string firstName;
public string FirstName
{
get { return firstName; }
set
{
firstName = value;
InvokePropertyChanged(new PropertyChangedEventArgs("FirstName"));
}
}
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
public void InvokePropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, e);
}
#endregion
}
Usage :
Sample sourceObject = new Sample();
textbox.DataBindings.Add("Text",sourceObject,"FirstName");
sourceObject.FirstName = "Stack";
A simplified version of the accepted answer that does NOT require you to type names of properties manually in every property setter like OnPropertyChanged("some-property-name"). Instead you just call OnPropertyChanged() without parameters:
You need .NET 4.5 minimum.
CallerMemberName is in the System.Runtime.CompilerServices namespace
public class Sample : INotifyPropertyChanged
{
private string _propString;
private int _propInt;
//======================================
// Actual implementation
//======================================
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
//======================================
// END: actual implementation
//======================================
public string PropString
{
get { return _propString; }
set
{
// do not trigger change event if values are the same
if (Equals(value, _propString)) return;
_propString = value;
//===================
// Usage in the Source
//===================
OnPropertyChanged();
}
}
public int PropInt
{
get { return _propInt; }
set
{
// do not allow negative numbers, but always trigger a change event
_propInt = value < 0 ? 0 : value;
OnPropertyChanged();
}
}
}
Usage stays the same:
var source = new Sample();
textbox.DataBindings.Add("Text", source, "PropString");
source.PropString = "Some new string";
Hope this helps someone.

Categories

Resources